简体   繁体   English

使用JSONObject进行json解析

[英]json parsing using JSONObject

My data is like this: 我的数据是这样的:

   {
       "region":  
             ["{'price':'119','volume':'20000','pe':'0','eps':'4.22','week53low':'92','week53high':'134.4','daylow':'117.2','dayhigh':'119.2','movingav50day':'115','marketcap':'0','time':'2015-11-25 05:13:34.996'}",   
              "{'price':'112','volume':'20000','pe':'0','eps':'9.22','week53low':'92','week53high':'134.4','daylow':'117.2','dayhigh':'119.2','movingav50day':'115','marketcap':'0','time':'2015-11-25 05:13:34.996'}",   
              "{'price':'118','volume':'20000','pe':'0','eps':'1.22','week53low':'92','week53high':'134.4','daylow':'117.2','dayhigh':'119.2','movingav50day':'115','marketcap':'0','time':'2015-11-25 05:13:34.996'}"  
              ]  
    }  

I am doing like below; 我像下面这样

        JSONObject jsonObj = new JSONObject(jsonString);
        JSONArray regionArray = jsonObj.getJSONArray("region");  

How do I get each price.. 我如何获得每个价格。

for (int i = 0; i < regionArray.length(); i++) {

             JSONObject item = regionArray.getJSONObject(i); 
             System.out.println(item.getString("price"));
        }  

Caused by: com.gemstone.org.json.JSONException: JSONArray[0] is not a JSONObject.

You have an array of Strings, not of JSON objects, so you can't do JSON object methods on it. 您有一个字符串数组,而不是JSON对象数组,因此无法在其上执行JSON对象方法。

In order for your code to work, your JSON would have to look like this: 为了使您的代码正常工作,您的JSON必须如下所示:

{
  "region": [
    {
      "price": "119",
      "volume": "20000",
      "pe": "0",
      "eps": "4.22",
      "week53low": "92",
      "week53high": "134.4",
      "daylow": "117.2",
      "dayhigh": "119.2",
      "movingav50day": "115",
      "marketcap": "0",
      "time": "2015-11-25 05:13:34.996"
    },
    {
      "price": "112",
      "volume": "20000",
      "pe": "0",
      "eps": "9.22",
      "week53low": "92",
      "week53high": "134.4",
      "daylow": "117.2",
      "dayhigh": "119.2",
      "movingav50day": "115",
      "marketcap": "0",
      "time": "2015-11-25 05:13:34.996"
    },
    {
      "price": "118",
      "volume": "20000",
      "pe": "0",
      "eps": "1.22",
      "week53low": "92",
      "week53high": "134.4",
      "daylow": "117.2",
      "dayhigh": "119.2",
      "movingav50day": "115",
      "marketcap": "0",
      "time": "2015-11-25 05:13:34.996"
    }
  ]
}

I just replaced that automatically in a text editor. 我只是在文本编辑器中自动替换了它。 You can actually let the numbers out of their "" enclosure if you want. 如果需要,您实际上可以将数字从其“”外壳中取出。

Have a look at it here: http://www.jsoneditoronline.org/ 在这里看看: http : //www.jsoneditoronline.org/

And here's some info on Json: http://www.json.org/ 这是有关Json的一些信息: http : //www.json.org/

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

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