解析JSON数组 [英] Parse JSON Array
本文介绍了解析JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的JSON是这样的:
My JSON is something like this:
[{
"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("--");
}
我无法读取data数组下的值。得到此错误
I am unable to read the values under the "data" array. Get this error
线程main中的异常
org.json.JSONException:JSONObject
文本必须开始带有'{'字符
1
Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at character 1
推荐答案
你正在尝试创建基于不表示对象的字符串的JSONObject,但是包含一个对象的数组。
You're trying to create a JSONObject based on a string that doesn't represent an object, but an array containing one object.
要获取包含的对象,请尝试
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.
这篇关于解析JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文