Doraemon 24 Doraemon 24 - 3 months ago 7
JSON Question

Android.Filtering using if statement getting data from JSON

I am reading all data from a

json
file but I want to filter out all "Delivered" delivery_status. So I use
if(status == jobj.getString(DELIVERY_STATUS))
. BUT NOTHING WORKS. please help me

The output MUST BE only:

{"delivery_id":"1","client_firstname":"Jhonny",delivery_status":"Pending"}
{"delivery_id":"4","client_firstname":"Marie",delivery_status":"On the Road"}


AndroidActivity.java (Code Snippet)

public static final String DELIVERY_ID = "delivery_id";
public static final String CLIENT_FIRSTNAME = "client_firstname";
public static final String DELIVERY_STATUS = "delivery_status";

JSONArray ja = response.getJSONArray("delivery");

for (int i = 0; i < ja.length(); i++) {
JSONObject jobj = ja.getJSONObject(i);
HashMap<String, String> item = new HashMap<String, String>();
String status = "Delivered";

if(status == jobj.getString(DELIVERY_STATUS)) {
item.put(DELIVERY_ID, jobj.getString(DELIVERY_ID));
item.put(CLIENT_FIRSTNAME, jobj.getString(CLIENT_FIRSTNAME));
item.put(DELIVERY_STATUS, jobj.getString(DELIVERY_STATUS));
}

Item_List.add(item);
} // for loop e


.json file

"delivery":[{"delivery_id":"1","client_firstname":"Jhonny",delivery_status":"Pending"}
{"delivery_id":"2","client_firstname":"Mario",delivery_status":"Delivered"}
{"delivery_id":"3","client_firstname":"Jason",delivery_status":"Delivered"}
{"delivery_id":"4","client_firstname":"Marie",delivery_status":"On the Road"}]

Answer

For this you have to do like this,

     if(jobj.getString("delivery_status").equals("Delivered")){

      //your code
     }
Comments