0

我在解析这个 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 多个时,它的任务太长了。我需要一种系统且有效的方法来做到这一点。请建议我对此的任何解决方案。

4

1 回答 1

0

你的问题是你得到一个字符串数组作为你的名字,然后试图将 JSONObjects 从同一个数组中提取出来。该数组仅包含字符串,正如您的堆栈跟踪所证明的那样。您需要遍历您的名称列表,并且对于从 objNames 数组中获得的每个名称,执行 ajsonObj.get(name)并将 THAT 转换为 a JSONObject

于 2012-05-03T20:49:49.983 回答