2012-03-15 144 views
0

我想解析这个JSON但它没有数组,我会得到计数。解析这个JSON

为我所用分析 如果JSONwill是:

{ 
    "earthquakes": [ 
     { 
      "eqid": "c0001xgp", 
      "magnitude": 8.8, 
      "lng": 142.369, 
      "src": "us", 
      "datetime": "2011-03-11 04:46:23", 
      "depth": 24.4, 
      "lat": 38.322 
     }, 
     { 
      "eqid": "2007hear", 
      "magnitude": 8.4, 
      "lng": 101.3815, 
      "src": "us", 
      "datetime": "2007-09-12 09:10:26", 
      "depth": 30, 
      "lat": -4.5172 
     }, 
     { 
      "eqid": "2007aqbk", 
      "magnitude": 8, 
      "lng": 156.9567, 
      "src": "us", 
      "datetime": "2007-04-01 18:39:56", 
      "depth": 10, 
      "lat": -8.4528 
     }, 
     { 
      "eqid": "2007hec6", 
      "magnitude": 7.8, 
      "lng": 100.9638, 
      "src": "us", 
      "datetime": "2007-09-12 21:49:01", 
      "depth": 10, 
      "lat": -2.5265 
     }, 
     { 
      "eqid": "a00043nx", 
      "magnitude": 7.7, 
      "lng": 100.1139, 
      "src": "us", 
      "datetime": "2010-10-25 12:42:22", 
      "depth": 20.6, 
      "lat": -3.4841 
     }, 
     { 
      "eqid": "2010utc5", 
      "magnitude": 7.7, 
      "lng": 97.1315, 
      "src": "us", 
      "datetime": "2010-04-06 20:15:02", 
      "depth": 31, 
      "lat": 2.3602 
     }, 
     { 
      "eqid": "2009mebz", 
      "magnitude": 7.6, 
      "lng": 99.9606, 
      "src": "us", 
      "datetime": "2009-09-30 08:16:09", 
      "depth": 80, 
      "lat": -0.7889 
     }, 
     { 
      "eqid": "2009kdb2", 
      "magnitude": 7.6, 
      "lng": 92.9226, 
      "src": "us", 
      "datetime": "2009-08-10 17:55:39", 
      "depth": 33.1, 
      "lat": 14.0129 
     }, 
     { 
      "eqid": "2010zbca", 
      "magnitude": 7.6, 
      "lng": 123.533, 
      "src": "us", 
      "datetime": "2010-07-23 20:51:11", 
      "depth": 576.3, 
      "lat": 6.4939 
     }, 
     { 
      "eqid": "2010xkbv", 
      "magnitude": 7.5, 
      "lng": 91.9379, 
      "src": "us", 
      "datetime": "2010-06-12 17:26:50", 
      "depth": 35, 
      "lat": 7.7477 
     } 
    ] 
} 

代码:

JSONObject json = JSONfunctions.getJSONfromURL("url"); 
JSONArray earthquake= json.getJSONArray("earthquakes"); 

,但我有这种类型的

[ 
    { 
     "id": "4", 
     "head": "gggg", 
     "details": "gdhghfhgfh", 
     "d2": "jkjkjk", 
     "datetime": "2012-03-12", 
     "last_update": "2012-03-14 05:08:32" 
    }, 
    { 
     "id": "5", 
     "head": "bb1", 
     "details": "sddassa", 
     "d2": "ddsdsddsd", 
     "datetime": "0000-00-00", 
     "last_update": "2012-03-13 07:33:56" 
    }, 
    { 
     "id": "3", 
     "head": "hhh", 
     "details": "hhhh", 
     "d2": "dsdsdds", 
     "datetime": "2012-03-01", 
     "last_update": "2012-03-12 08:35:27" 
    } 
] 

有没有像“地震“。

+1

它的Json Jason不把它转换成JSONArray:P – 2012-03-15 08:09:24

+0

烨得到它.... :) :) – zaiff 2012-03-15 09:40:26

回答

1

您可以直接转换服务器的响应数据的数组,那么你可以将其直接使用JSONArray arr = new JSONArray(stringResponse);

如果你转换成JSONArray在字符串JSON响应,那么你可以按照如下

 String stringResponse = JSONfunctions.getJSONResponsefromURL("url"); 
     try { 
      JSONArray arr = new JSONArray(stringResponse); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
2

当然,因为您要求输入关键“地震”的JSON数组,现在您得到了与关键“地震”匹配的JSON数组。

刚才我明白你的问题。 JSON库不太可能会更改内容,它更有可能从url中检索的内容每次都有点不同。为了确保这样的话,试试这个代码:

JSONObject json = JSONfunctions.getJSONfromURL("url"); 
Log.d("Some tag", json.toString()); 
JSONArray earthquake= json.getJSONArray("earthquakes"); 
Log.d("Some tag", earthquake.toString()); 
+1

,但如果你在JSON字符串仔细观察,你”会发现异常 – waqaslam 2012-03-15 08:09:46

+0

我后来看到了,请看我的编辑。 – MByD 2012-03-15 08:10:11