【发布时间】:2020-03-27 10:41:02
【问题描述】:
我正在尝试从同一个请求中读取两个 json 数组
JSON 文件:
{
"items": [
{
"title": "welcoem home",
"author": "Charles Dickens"
},
{
"title": "Harry Potter",
"author": "J rowling"
}]
}
{
"items1": [
{
"title": "welcoem home",
"author": "Charles Dickens"
},
{
"title": "Harry Potter",
"author": "J rowling"
}]
}
我用来阅读的代码是:
//Convert JSON to String and gets our app version
private String getJSON(String JSON_STRING){
try {
//Objects
jsonObject=new JSONObject(JSON_STRING);
jsonArray=jsonObject.getJSONArray("items");
//Variables
int count=0;
String tempTitle, temAuthor;
while (count<jsonArray.length()){
//Reads Row by Row
JSONObject JO= jsonArray.getJSONObject(count);
tempTitle= JO.getString("title");
temAuthor= JO.getString("author");
//If our App found
if (Constants.Title.equals(tempTitle)){
return temAuthor;
}
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
return "Not Found!";
}
但是当我调用读取第二个表(如 items1)时,它说没有找到表? 我试图在表之间添加一个 (,) 但相同,我是 php 和 android 的新手,所以我很难更改代码,所以这对我来说是任何简单的方法,我的意思是任何改变更少代码的方法让它工作,或者我必须完全改变我的android方法?
【问题讨论】:
-
您的
json格式错误。先改正
标签: android arrays json parsing