我在解析这个 JSON 时遇到问题:
{
"850": {
"display-name": "Volvo 850",
"name-parts": {
"make": "volvo",
"model": "850"
},
"image": "http://images.thecarconnection.com/tmb/1997-volvo-850-lp_100026906_t.gif",
"url": "http://www.thecarconnection.com/cars/volvo_850"
},
"960": {
"display-name": "Volvo 960",
"name-parts": {
"make": "volvo",
"model": "960"
},
"image": "http://images.thecarconnection.com/tmb/1997-volvo-960_100026908_t.gif",
"url": "http://www.thecarconnection.com/cars/volvo_960"
},
"c30": {
"display-name": "Volvo C30",
"name-parts": {
"make": "volvo",
"model": "c30"
},
"image": "http://images.thecarconnection.com/tmb/2012-volvo-c30-2-door-coupe-auto-angular-front-exterior-view_100358956_t.gif",
"url": "http://www.thecarconnection.com/cars/volvo_c30"
},
"c70": {
"display-name": "Volvo C70",
"name-parts": {
"make": "volvo",
"model": "c70"
},
"image": "http://images.thecarconnection.com/tmb/2012-volvo-c70_100369317_t.gif",
"url": "http://www.thecarconnection.com/cars/volvo_c70"
},
"s40": {
"display-name": "Volvo S40",
"name-parts": {
"make": "volvo",
"model": "s40"
},
"image": "http://images.thecarconnection.com/tmb/2011-volvo-s40-4-door-sedan-angular-front-exterior-view_100329062_t.gif",
"url": "http://www.thecarconnection.com/cars/volvo_s40"
}
}
我正在解析相同的内容:
try{
JSONObject jsonObj = new JSONObject(jsonString);
System.out.println(jsonObj.length());
JSONArray objNames = jsonObj.names();
for(int i=0;i<objNames.length();i++)
{
System.out.println("The Name:=========="+objNames.getString(i));
}
if(jsonObj.length()>0){
for (int index = 0; index < jsonObj.length(); index++) {
JSONObject jsonName = (JSONObject) objNames.get(index);
System.out.println("The display name:"+jsonName.getString((String) objNames.get(index)));
System.out.println("The Image:"+jsonName.getString("image"));
System.out.println("The URL:"+jsonName.getString("url"));
}
}
}catch(Exception e)
{
e.printStackTrace();
}
但它返回一个错误:
05-04 01:59:54.737: W/System.err(3850): org.json.JSONException: Value tl at 0 of type java.lang.String cannot be converted to JSONObject
05-04 01:59:54.737: W/System.err(3850): at org.json.JSON.typeMismatch(JSON.java:96)
05-04 01:59:54.737: W/System.err(3850): at org.json.JSONArray.getJSONObject(JSONArray.java:484)
05-04 01:59:54.747: W/System.err(3850): at com.TCC.android.parse.JsonParse.parseBrands(JsonParse.java:30)
05-04 01:59:54.747: W/System.err(3850): at com.TCC.android.ResearchList$2$2.run(ResearchList.java:167)
我正在尝试解决这个问题,但每次都失败了,我可以通过每次获取 Json 对象并解析它的值来做到这一点,但是当对象增加到 100 多个时,它的任务太长了。我需要一种系统且有效的方法来做到这一点。请建议我对此的任何解决方案。