2017-04-18 89 views
-1

我只需要解碼description,temp,id和name!我怎麼能這樣做?multilevel jsonObject解析

{ 
    "cnt": 1, 
    "list": [ 
     { 
      "coord": { 
      }, 
      "sys": { 
      }, 
      "weather": [ 
       { 
        "id": 800, 
        "main": "Clear", 
        "description": "clear sky", 
        "icon": "01n" 
       } 
      ], 
      "main": { 
       "temp": 0.02, 
       "pressure": 1025, 
       "humidity": 68, 
       "temp_min": -1, 
       "temp_max": 1 
      }, 
      "visibility": 10000, 
      "wind": { 
      }, 
      "clouds": { 
      }, 
      "dt": 1492539576, 
      "id": 524901, 
      "name": "Moscow" 
     } 
    ] 
} 
+0

你已經在你的JSON兩個 「id's」,你要哪一個? – AlexGuerra

+0

我需要第二個「ID」 –

回答

1

使用此代碼:

try{ 
    JSONObject json = new JSONObject(jsonString); 
    JSONObject list = json.getJSONArray("list"); 
    JSONObject object = list.getJSONObject(0); 
    JSONArray weatherArray = object.getJSONArray("weather"); 
    for(int i=0;i<weatherArray.length();i++){ 
     JSONObject object_n = weatherArray.getJSONObject(i); 
     String description = object_n.getString("description"); 
     } 
    JSONObject main = object.getJSONObject("main"); 
    String temp = main.getString("temp"); 
    String name = object.getString("name"); 
    String id = object.getString("id"); 
    } 
    catch (JSONException e){ 
      e.printStackTrace(); 
    } 
+0

工作!謝謝:) –

+0

歡迎您...請務必將答案標記爲正確..thankx – rafsanahmad007

0

嘗試這種情況:

// Here is your sample JSON 
    String sampleJson = "{\n" + 
      " \"cnt\": 1,\n" + 
      " \"list\": [\n" + 
      "  {\n" + 
      "   \"coord\": {\n" + 
      "   },\n" + 
      "   \"sys\": {\n" + 
      "   },\n" + 
      "   \"weather\": [\n" + 
      "    {\n" + 
      "     \"id\": 800,\n" + 
      "     \"main\": \"Clear\",\n" + 
      "     \"description\": \"clear sky\",\n" + 
      "     \"icon\": \"01n\"\n" + 
      "    }\n" + 
      "   ],\n" + 
      "   \"main\": {\n" + 
      "    \"temp\": 0.02,\n" + 
      "    \"pressure\": 1025,\n" + 
      "    \"humidity\": 68,\n" + 
      "    \"temp_min\": -1,\n" + 
      "    \"temp_max\": 1\n" + 
      "   },\n" + 
      "   \"visibility\": 10000,\n" + 
      "   \"wind\": {\n" + 
      "   },\n" + 
      "   \"clouds\": {\n" + 
      "   },\n" + 
      "   \"dt\": 1492539576,\n" + 
      "   \"id\": 524901,\n" + 
      "   \"name\": \"Moscow\"\n" + 
      "  }\n" + 
      " ]\n" + 
      "}"; 

使用方法parseMultilevelJSON(sampleJson)到從上述數據sampleJson解析descriptiontempidname

public void parseMultilevelJSON(String jsonStr) { 

    if (jsonStr != null) { 
     try { 
      JSONObject jsonObj = new JSONObject(jsonStr); 

      // List 
      JSONArray listJsonArray = jsonObj.getJSONArray("list"); 
      // List-Item 
      JSONObject itemJsonObject = listJsonArray.getJSONObject(0); 

      // Weather 
      JSONArray weatherJsonArray = itemJsonObject.getJSONArray("weather"); 
      // Weather-Item 
      JSONObject weatherJsonObject = weatherJsonArray.getJSONObject(0); 

      // Description 
      String description = weatherJsonObject.getString("description"); 

      // Main 
      JSONObject mainJsonObject = itemJsonObject.getJSONObject("main"); 
      // Temp 
      double temp = mainJsonObject.getDouble("temp"); 

      // Id 
      int id = itemJsonObject.getInt("id"); 
      // Name 
      String name = itemJsonObject.getString("name"); 

      Log.d("SUCCESS", "JSON ITEM: " + "\nDescription: " + description 
        + "\nTemp: " + temp + "\nId: " + id + "\nName: " + name); 

     } catch (final JSONException e) { 
      Log.e("FAILED", "Json parsing error: " + e.getMessage()); 
     } 
    } 
} 

OUTPUT:

3313-3313/com.ferdous.stackoverflowanswer D/SUCCESS: JSON ITEM: 
                Description: clear sky 
                Temp: 0.02 
                Id: 524901 
                Name: Moscow