简体   繁体   English

将字符串转换为JsonObject

[英]Convert String to JsonObject

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

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. 我正在尝试解决此问题,但是每次都失败,我可以每次获取Json对象并解析其值时执行此操作,但是当Objects增加到100个以上时,它的任务太长了。我需要一种系统且有效的方法来做到这一点。 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. 您的问题是您要获取一个字符串数组作为名称,但是随后尝试将JSONObjects从同一数组中拉出。 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 . 您需要遍历名称列表,对于从objNames数组中获得的每个名称,请执行jsonObj.get(name)并将其THAT转换为JSONObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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