Android.Filtering使用if语句从JSON获取数据

时间:2021-01-25 19:33:44

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

我正在读取json文件中的所有数据,但我想过滤掉所有“Delivered”delivery_status。所以我使用if(status == jobj.getString(DELIVERY_STATUS))。但是没有工作。请帮帮我

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)

AndroidActivity.java(代码片段)

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"}]

1 个解决方案

#1


1  

For this you have to do like this,

为此,你必须这样做,

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

      //your code
     }

#1


1  

For this you have to do like this,

为此,你必须这样做,

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

      //your code
     }