Java 解析 JSON 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6402115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse JSON Array
提问by KMJ
My JSON is something like this:
我的 JSON 是这样的:
[{
"myviews":[{
"2011-05-12_2011-05-14":{
"name":"thiswk",
"data":[[12,
2403
],
[13,
2082
],
[14,
5823
]
]
}
},
{
"2011-06-05_2011-06-7":{
"name":"lastwk",
"data":[[5,
1279
],
[6,
6685
],
[7,
2163
]
]
}
}
]
}
]
JSONObject jo = new JSONObject(jsonString);
JSONArray ja;
jo = jo.getJSONObject("2011-05-12_2011-05-14");
ja = jo.getJSONArray("data");
int resultCount = ja.length();
for (int i = 0; i < resultCount; i++)
{
JSONObject resultObject = ja.getJSONObject(i);
resultObject.getJSONArray("12");
System.out.println("--");
}
I am unable to read the values under the "data" array. Get this error
我无法读取“数据”数组下的值。得到这个错误
Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at character 1
线程“main”org.json.JSONException 中的异常:JSONObject 文本必须在字符 1 处以“{”开头
回答by Paul Tomblin
data
appears to be an array of arrays. Perhaps you need to call ja.getJSONArray(i)
?
data
似乎是一个数组数组。也许你需要打电话ja.getJSONArray(i)
?
回答by Don Roby
You're trying to create a JSONObject based on a string that doesn't represent an object, but an array containing one object.
您正在尝试基于不表示对象的字符串创建 JSONObject,而是基于包含一个对象的数组。
To get the contained object, try
要获取包含的对象,请尝试
JSONArray inputArray = new JSONArray(jsonString);
JSONObject jo = inputArray.getJSONObject(0);
I think some of your later work is wrong as well, but perhaps this will get you started.
我认为你后来的一些工作也是错误的,但也许这会让你开始。