将String转换为JsonObject

时间:2012-05-03 20:44:21

标签: android json

我有一个问题要解析这个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以上时它的任务太长了。我需要一个系统而有效的方法来去做。请建议我解决这个问题。

1 个答案:

答案 0 :(得分:0)

你的问题是你得到一个字符串数组作为你的名字,但后来尝试从同一个数组中拉出JSONObjects。该数组只包含字符串,如堆栈跟踪所示。您需要遍历您的名称列表以及从objNames数组中获取的每个名称,执行jsonObj.get(name)并将THAT转换为JSONObject