我需要帮助...我有一个 php 文件,它返回两个 json 数组,如下所示:
[{ "id":"1",
"item":"hammers",
"aisle":"20"
}
{ "id":"1",
"item":"hammers",
"aisle":"20"
}]
[{ "id":"1",
"itemFound":"Your item #item",
"ThankYou":"and Thank You for using Txtcore!"
}]
现在,我想获取 Android 中的第二个数组项。我现在有以下代码: JSONArray jsonArray = new JSONArray(结果);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
items.add(jsonObject.getString("item"));
aisles.add(""+jsonObject.getString("item");
}
但显然,这会返回第一个数组中的对象。我想从第二个数组中获取对象。任何建议。
最佳答案
您的 JSON 无效,但您可以按如下方式获取您的元素。这是许多人的解决方案(只是一个解决方案)。
String str = "YOUR_JSON_RESPONSE";
String array[] = str.split("\\[\\{");//
try {
JSONArray jsonArray=new JSONArray("[{" + array[2]));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
关于java - 在android中解析json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23200878/