Erzha Emaszda Erzha Emaszda - 1 year ago 113
JSON Question

Fetch JSON Array to ListView Android

I have response from server like this :

enter image description here

How to fetch that response to :

ListOrderItem orderItem = new ListOrderItem();

orderItem.setId_order(order_id);

orderItem.setUser_phone(order_user_phone);

orderItem.setUser_name(order_user_name);

orderItem.setVehicle_type(order_vehicle_type);

orderItem.setVehicle_brand(order_vehicle_merk);

orderItem.setVehicle_year(order_vehicle_year);

orderItem.setNote(order_note);

orderItem.setLongitude(order_longitude);

orderItem.setLatitude(order_latitude);

orderItem.setLocation(order_location);

arrayOrder.add(orderItem);

Answer Source

Try something like this

JSONObject response = new JSONObject(new String(yourResponseString));
int flag = response.getInt("code");
if ( flag == 200 ) {

  JSONArray message = response.getJSONArray("message");
   for (int i = 0; i < message.length(); i++) {
       ListOrderItem orderItem = new ListOrderItem();
        JSONObject eachMessage = message.getJSONObject(i);
        orderItem.setId_order(eachMessage.getString("order_id"));
        orderItem.setUser_phone(eachMessage.getString("order_user_phone"));
        orderItem.setUser_name(eachMessage.getString("order_user_name"));
        orderItem.setVehicle_type(eachMessage.getString("order_vehicle_type"));
        orderItem.setVehicle_brand(eachMessage.getString("order_vehicle_merk"));
        orderItem.setVehicle_year(eachMessage.getString("order_vehicle_year"));
        orderItem.setNote(eachMessage.getString("order_note"));
        orderItem.setLongitude(eachMessage.getString("order_longitude"));
        orderItem.setLatitude(eachMessage.getString("order_latitude"));
        orderItem.setLocation(eachMessage.getString("order_location"));
        arrayOrder.add(orderItem);

   }

}