java - 如何使用 Volley 和 POJO 类在 Android 中解析嵌套的 JSON 对象
问题描述
{
"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
}
或者更好的是你可以使用改造。
推荐阅读
- javascript - 在 JS 中更改导入变量的值。一种方法有效,另一种方法无效,为什么会这样?
- javascript - 带有 React 的计时器无法读取未定义的属性秒数
- python - Tkinter ttk.Checkbox 不起作用
- django - 检索具有特定参与用户的所有聊天
- regex - 来自网络爬虫函数的这个正则表达式背后的逻辑是什么: ("^(http|www)((?!("+linkParse+")).)*$")
- git - 合并时 GIt 保持当前版本(我们的版本或他们的版本)
- unity3d - 如何手动安装对统一的 android 构建支持(统一集线器)?
- python - 如何在打印的表格中添加行号
- php - 通过元键排除 Wordpress 循环中的帖子
- wordpress - 如何将基于主题位置的 WordPress 菜单项作为简单的字符串/文本?