【发布时间】:2013-03-17 18:45:34
【问题描述】:
我在 Android 中有一个项目,这里有我需要解析的 JSON,以便我可以使用数据:(我尝试在 JSONLint.com 验证我的 JSON,一切都很好)
{
"traces": [
"{\"lat\": \"42.842097\", \"lng\": \"-31.258422\", \"idTrace\": \"444e8079\"}",
"{\"lat\": \"56.842097\", \"lng\": \"-73.258422\", \"idTrace\": \"088b591a\"}"
]
}
问题是当我尝试在 JSONArray 中获取 2 个 JSONObjects 时总是出错。
这是我的代码的 sn-p:
(...)
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;)
{
builder.append(line).append("\n");
}
JSONObject result = new JSONObject(builder.toString());
JSONArray list = result.getJSONArray("traces");
try{
for (int i = 0; i < list.length(); i++)
{ //this is where I get the error "JSONArray[0] is not a JSONObject"
JSONObject jsonObject = list.optJSONObject(i);
//String id = jsonObject.getString("idTrace");
//String lat = jsonObject.getString("lat");
//String lng = jsonObject.getString("lng");
(...)
}
(...)
我进行了很多搜索以找出问题所在,但人们似乎在做与我完全相同的事情......我找不到它不起作用的原因!
感谢您的帮助!
【问题讨论】:
-
尝试
list.optJSONObject(i);而不是liste.optJSONObject(i); -
循环中有错字。仅仅是输入问题还是您的实际代码?
-
我打错了,对不起,问题依旧
标签: android arrays json object