gpt4 book ai didi

java - 如何使用 Volley 和 POJO 类解析 Android 中的嵌套 JSON 对象

转载 作者:行者123 更新时间:2023-12-02 10:14:25 24 4
gpt4 key购买 nike

{
"status": "1",
"message": "",
"result": {
"info": {
"tax": "0",
"discount": "0",
"minimum_spend": "300",
"delivery_charges": "0",
"last_updated": "1 week 15 hours ago"
},
"items": [
{
"name": "Eat At Home",
"menu_item_id": "12345",
"menu_cat_id": "4321",
"menu_cat_sku": "",
"nutritions": "",
"price": "1000",
"currency": "USD",
"desc": "Desription",
"category": "Promotion",
"image": "https://static.google.com/media/images/thumbs/343e8f41b18325a6058adc3773ed4d53.png",
"large_image": "https://static.google.com/media/images/343e8f41b18325a6058adc3773ed4d53.png",
"options": [],
"discount": "",
"weight": "",
"sku": "",
"status": "0",
"brand": []
},
{
"name": "Lunch Bundle",
"menu_item_id": "4321",
"menu_cat_id": "4321",
"menu_cat_sku": "",
"nutritions": "",
"price": "1500",
"currency": "USD",
"desc": "Description",
"category": "Promotion",
"image": "https://static.google.com/media/images/thumbs/62cdde279bbc3e45b8456f040d649b32.png",
"large_image": "https://static.google.com/media/images/62cdde279bbc3e45b8456f040d649b32.png",
"options": [],
"discount": "",
"weight": "",
"sku": "",
"status": "0",
"brand": []
},

我的代码

MenuResponse menuResponse = JsonParser.getInstance().parseMenuResponse(response);


public MenuResponse parseMenuResponse(String serverResponse) throws Exception {

MenuResponse response = null;

if (serverResponse != null) {
try {
response = gson.fromJson(serverResponse, MenuResponse.class);
} catch (JsonSyntaxException jse) {
throw new Exception(ERROR_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
}
}

return response;
}

最佳答案

试试这个。

JSONObject json = new JSONObject(response);

String status= json.getString("status");
JSONArray itemsArray= json.getJSONArray("items");

for (int i = 0; i < itemsArray.length(); i++) {
JSONObject c = itemsArray.getJSONObject(i);
String name= c.getString("name");
//remaining data you will
}

或者更好的是,您可以使用 Retrofit。

关于java - 如何使用 Volley 和 POJO 类解析 Android 中的嵌套 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54779853/

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