2012-03-15 141 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