Android 如何从主数组的子数组中获取json值
我坚持用选项获取阵列 我的json看起来像这样Android 如何从主数组的子数组中获取json值,android,json,Android,Json,我坚持用选项获取阵列 我的json看起来像这样 { "data": [{ "id": "36", "combo_items": { "type": "", "combo": [""] } }, { "id": "14", "combo_items": { "type": "combo", "combo":
{
"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();
}
试试看{
JSONArray ja=response.getJSONArray(“数据”);
对于(int i=0;i 对于(int ii=0;ii您可以尝试opt
而不是get
并检查null
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) {
}
试试看{
JSONArray ja=response.optJSONArray(“数据”);
if(ja==null){
回来
}
对于(int i=0;i
这是JSON还是Gson,你从哪里得到这个响应?从我们的php端,你从这行“combo”得到错误:[“”]这是字符串数组,不是josn