如何从子阵列从主阵列获取JSON值
问题描述:
荫停留在越来越阵列 的json看起来像这样的选项,如何从子阵列从主阵列获取JSON值
{
"data": [{
"id": "36",
"combo_items": {
"type": "",
"combo": [""]
}
}, {
"id": "14",
"combo_items": {
"type": "combo",
"combo": [{
"id": "12",
"name": ""
}, {
"id": "15",
"name": "test"
}]
}
},
}]}
我已经尝试过... 喜欢这些
try {
JSONArray ja = response.getJSONArray("data");
for (int i = 0; i < ja.length(); i++)
{
jo = (JSONObject) ja.get(i);
String id = jo.getString("id");
JSONObject combo_items = jo.getJSONObject("combo_items");
String combo_type = combo_items.getString("type");
try {
JSONArray jsonArray = combo_items.getJSONArray("combo");
for (int ii =0; ii<jsonArray.length(); ii++){
jsonObject = (JSONObject) jsonArray.get(ii);
String combo_id = jsonObject.getString("id");
String combo_name=jsonObject.getString("name");
}
}catch (JSONException e){
e.printStackTrace();
}
如何从JSON数组内得到JSON对象,我想方法显示了一些错误 串canot被转换为JSONObject的 请帮助。
答
你可以试试opt
代替get
和检查空。
try {
JSONArray ja = response.optJSONArray("data");
if (ja == null) {
return;
}
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.optJSONObject(i);
if (jo == null) {
continue;
}
String id = jo.optString("id");
JSONObject combo_items = jo.optJSONObject("combo_items");
String combo_type = combo_items.optString("type");
try {
JSONArray jsonArray = combo_items.optJSONArray("combo");
if (jsonArray != null) {
for (int ii = 0; ii < jsonArray.length(); ii++) {
JSONObject jsonObject = jsonArray.optJSONObject(ii);
if (jsonObject == null) {
continue;
}
String combo_id = jsonObject.getString("id");
String combo_name = jsonObject.getString("name");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
}
+0
这是solved.Thanks – KpAbhijith
这是JSON或GSON从你在哪里得到这个响应 – Anil
JSON,从我们的PHP端 – KpAbhijith
您是从该行“组合”得到错误:“”]它是一个字符串数组不josn – Anil