gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:02:24 26 4
gpt4 key购买 nike

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

输出必须只有:

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

AndroidActivity.java(代码 fragment )

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 文件

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

最佳答案

为此你必须这样做,

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

//your code
}

关于Android.Filtering 使用 if 语句从 JSON 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39255197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com