简体   繁体   中英

Convert String to JsonObject

I have a problem to parse this 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"
  }
}

I am parsing the same like this:

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();
         }

But it returns an error as:

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)

I am trying to resolve this problem but everytime failed, I can do this by everytime get the Json object and parse its values but its too lengthy task when the Objects increased to more than 100. I need a systematic and efficient way to do that. Please suggest me any solution regarding that.

Your problem is that you are getting an array of strings as your names, but then trying to pull JSONObjects out of that same array. That array only contains strings, as evidenced by your stack trace. You need to iterate through your list of names and for each name you get out of the objNames array, do a jsonObj.get(name) and cast THAT to a JSONObject .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM