我是android的新手,虽然我在使用数组解析json数组时遇到问题。
{
"menu": [
{
"Soups": [
{
"name": "Safed Tamatar aur Tulsi ",
"price": 150,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Dum Murg",
"price": 168,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Murg Zaffrani",
"price": 168,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Tomato Dhania",
"price": 150,
"description": "",
"veg": true,
"spicy": null
}
]
},
{
"Starters": [
{
"name": "Achari Paneer tikka",
"price": 347,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Paneer Tikka",
"price": 347,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Murg Tikka",
"price": 393,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Tandoori Murg (Half)",
"price": 410,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Tandoori Murg (Full)",
"price": 851,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Macchi Tikka",
"price": 626,
"description": "",
"veg": false,
"spicy": null
}
]
},
{
"Main Course": [
{
"name": "Jeera Aloo",
"price": 275,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Paneer Tikka Masala",
"price": 392,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Palak Paneer",
"price": 370,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Murg Kadhai",
"price": 428,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Murg Tikka Masala",
"price": 428,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Palak Gosht",
"price": 455,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Macchi Kadhai",
"price": 545,
"description": "",
"veg": false,
"spicy": null
}
]
},
{
"Rice": []
},
{
"Breads": []
},
{
"Accompaniments": []
},
{
"Beverages": []
},
{
"Dessert": []
},
{
"Signature Mocktails": []
},
{
"Wraps": []
},
{
"Lentils": [
{
"name": "Dal Tadka",
"price": 226,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Dal Kolhapuri",
"price": 226,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Dal Makhani",
"price": 275,
"description": "",
"veg": true,
"spicy": null
}
]
}
],
"name": "Zaffran"
}
我想解析这个文件,但是互联网上没有这样的代码可以通过GSON解析这个文件。 我使用了以下代码:
JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue();
JSONArray jsonArray = jsonObject.getJSONArray("menu");
String jsonString = jsonArray.toString();
Type type = new TypeToken<Map<String, String>>() {
}.getType();
Map<String, String> myMap = new Gson().fromJson(jsonString, type);
for (Map.Entry<String, String> entry : myMap.entrySet()) {
key = entry.getKey();
value = entry.getValue();
// do stuff
}
responseView.setText(value);
但是,它给出了String期望找到数组的错误。请帮帮我
答案 0 :(得分:0)
这不是完美的答案,但我试图向您展示解析嵌套json的格式。这将为您提供json数组&#34;菜单&#34;及其内部数组也。
根据您的需要进行更改。
JSONObject jsonMenu = mJsonObject.optJSONObject("menu");
JSONArray jsnSoupsarr = jsonMenu.optJSONArray("Soups");
for (int k = 0; k < jsnSoupsarr.length(); k++) {
JSONObject jsnSoup = jsnMSouparr.optJSONObject(k);
//here get the inner fields of "Soup" like "price","name" etc.
}
JSONArray startersJson = jsnMenu.optJSONArray("Starters");
for (int k = 0; k < starterJson.length(); k++) {
JSONObject objstarter = starterJson.optJSONObject(k);
}
JSONArray mainCourseJson = jsnMenu.optJSONArray("Main Course");
for (int k = 0; k < mainCourseJson.length(); k++) {
JSONObject objmainCourse = mainCourseJson.optJSONObject(k);}
希望它有所帮助!
答案 1 :(得分:0)
使用以下代码解析完整的json数据。
try {
JSONObject jsonObject = new JSONObject(jsonArray);
JSONArray jsonArray1 = jsonObject.getJSONArray("menu");
Menu menu = new Menu();
if (jsonObject.has("name")) {
menu.setName(jsonObject.getString("name"));
} else {
menu.setName("");
}
List<MenuHeaders> menuHeadersList = new ArrayList<>();
for (int i = 0; i < jsonArray1.length(); i++) {
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
MenuHeaders menuHeaders = new MenuHeaders();
//Creating list to save all prices and item names
List<ItemDetails> list = new ArrayList<>();
JSONArray subHeaderJsonArray = null;
if (jsonObject1.has("Soups")) {
menuHeaders.setTitle("Soups");
subHeaderJsonArray = jsonObject1.getJSONArray("Soups");
}
if (jsonObject1.has("Starters")) {
menuHeaders.setTitle("Starters");
subHeaderJsonArray = jsonObject1.getJSONArray("Starters");
}
if (jsonObject1.has("Main Course")) {
menuHeaders.setTitle("Main Course");
subHeaderJsonArray = jsonObject1.getJSONArray("Main Course");
}
if (jsonObject1.has("Rice")) {
menuHeaders.setTitle("Rice");
subHeaderJsonArray = jsonObject1.getJSONArray("Rice");
}
if (jsonObject1.has("Breads")) {
menuHeaders.setTitle("Breads");
subHeaderJsonArray = jsonObject1.getJSONArray("Breads");
}
if (jsonObject1.has("Accompaniments")) {
menuHeaders.setTitle("Accompaniments");
subHeaderJsonArray = jsonObject1.getJSONArray("Accompaniments");
}
//Add all other sub menu items to menu object
if (jsonObject1.has("Lentils")) {
menuHeaders.setTitle("Lentils");
subHeaderJsonArray = jsonObject1.getJSONArray("Lentils");
}
if (subHeaderJsonArray != null) {
for (int index1 = 0; index1 < subHeaderJsonArray.length(); index1++) {
JSONObject subHeaderJsonObject = subHeaderJsonArray.getJSONObject(i);
ItemDetails itemDetails = new ItemDetails();
itemDetails.setName(subHeaderJsonObject.getString("name"));
itemDetails.setPrice(subHeaderJsonObject.getInt("price"));
itemDetails.setDescription(subHeaderJsonObject.getString("description"));
itemDetails.setVeg(subHeaderJsonObject.getBoolean("veg"));
itemDetails.setSpicy(subHeaderJsonObject.getString("spicy"));
list.add(itemDetails);
}
menuHeaders.setList(list);
}
menuHeadersList.add(menuHeaders);
menu.setMenuHeaders(menuHeadersList);
}
} catch (JSONException e) {
e.printStackTrace();
}