2014-01-09 88 views
-4

解析位於互聯網上的android json文件時遇到了一些困難。 這裏是JSON文件的內容:Android解析困難

{ 
    "cod": "200", 
    "message": 0.0803, 
    "city": { 
     "id": 2643743, 
     "name": "London", 
     "coord": { 
      "lon": -0.12574, 
      "lat": 51.50853 
     }, 
     "country": "GB", 
     "population": 1000000 
    }, 
    "cnt": 2, 
    "list": [ 
     { 
      "dt": 1389182400, 
      "temp": { 
       "day": 9.72, 
       "min": 9.72, 
       "max": 11.25, 
       "night": 11.25, 
       "eve": 9.72, 
       "morn": 9.72 
      }, 
      "pressure": 1016.8, 
      "humidity": 95, 
      "weather": [ 
       { 
        "id": 500, 
        "main": "Rain", 
        "description": "light rain", 
        "icon": "10d" 
       } 
      ], 
      "speed": 6, 
      "deg": 162, 
      "clouds": 80, 
      "rain": 2 
     }, 
     { 
      "dt": 1389268800, 
      "temp": { 
       "day": 9.75, 
       "min": 4.56, 
       "max": 11.22, 
       "night": 4.56, 
       "eve": 6.76, 
       "morn": 11.22 
      }, 
      "pressure": 1006.76, 
      "humidity": 95, 
      "weather": [ 
       { 
        "id": 500, 
        "main": "Rain", 
        "description": "light rain", 
        "icon": "10d" 
       } 
      ], 
      "speed": 9.16, 
      "deg": 267, 
      "clouds": 44, 
      "rain": 0.5 
     } 
    ] 
} 

我想一些幫助,因爲我不知道如何解析列表。我不斷收到應用程序崩潰。

+1

發佈您產生此「崩潰」的代碼。 – csmckelvey

回答

1

你必須給我們更多的幫助:你有沒有崩潰日誌?你有什麼嘗試?

jsonlint.com確認您的JSON有效。如果我理解你的意圖,你可以按如下deserialise這一個JSONObject

double speed = obj.getJSONArray("list") 
          .getJSONObject(2) 
          .getDouble("speed"); 
+0

我只是想獲得列表數組中的所有鍵。這裏是我的代碼的一部分,我清理了沒有崩潰: –

+0

JSONObject json = jParser.getJSONFromUrl(url); \t String strParsedValue = null; \t嘗試{ \t \t \t \t JSONArray jsonArray = json.getJSONArray(「list」); \t \t \t \t對(INT I = 0; I

+0

@YanP是對 「壓力」 真是一個字符串值?同上「溼度」? – SK9

0

創建一個JSON對象進行字符串:

JSONObject obj = new JSONObject(myString); 

一旦你有了這個,你可以按如下方法訪問值然後調用你正在尋找的作品的get方法。例如:

JSONObject jsonObject = new JSONObject(jsonString); 
Date myDOB = new Date(jsonObject.getLong(DOB_KEY)); 

對於嵌入進一步的部分,您可以調用getJSONObject(key),然後分別解析該部分。例如:

JSONObject location = jsonObject.getJSONObject(LOCATION_KEY); 
myLocation = new Location(""); 
myLocation.setLatitude(location.getDouble(LOCATION_LATITUDE_KEY)); 
myLocation.setLongitude(location.getDouble(LOCATION_LONGITUDE_KEY));