2016-02-17 64 views
0

我正在解析從JSON格式的url收到的響應字符串。代碼停在創建JSONArray的行。它不打印任何日誌或例外。當我嘗試調試時,我可以看到JSONArray被創建爲正確的值,但隨後代碼在稍後的行中突然停止。該代碼段是 -運行android代碼時未創建JSONArray

private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays) 
       throws JSONException 

{ 

    JSONObject forecastJson = new JSONObject(forecastJsonStr); 
    Log.v("Jsonobject forcastJson", "done"); 
    JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); 
    Log.v("JsonArray", "done"); 
    for (int i=0; i< weatherArray.length();i++){ 
     JSONObject daydata = weatherArray.getJSONObject(i); 
     Log.v("daydata", "done"); 
     JSONObject weatherobject = daydata.getJSONObject(OWM_WEATHER); 
     Log.v("weatherobject", "done"); 
     String description = weatherobject.getString(OWM_DESCRIPTION); 

     JSONObject tempratureobject = daydata.getJSONObject(OWM_DESCRIPTION); 
     Log.v("tempratureobject", "done"); 
     double high = tempratureobject.getDouble(OWM_MAX); 
     double low = tempratureobject.getDouble(OWM_MIN); 
     highAndLow = formatHighLows(high, low); 

的JSON是 -

{ 
"city": 
{ 
    "id": 5103269, 
    "name": "Ridgewood", 
    "coord": 
    { 
     "lon": -74.116531, 
     "lat": 40.979259 
    }, 
    "country": "US", 
    "population": 0, 
    "sys": 
    { 
     "population": 0 
    } 
}, 
"cod": "200", 
"message": 0.0292, 
"cnt": 7, 
"list": [ 
{ 
    "dt": 1455742800, 
    "main": 
    { 
     "temp": 2.98, 
     "temp_min": 2.98, 
     "temp_max": 4.04, 
     "pressure": 1025.94, 
     "sea_level": 1029.53, 
     "grnd_level": 1025.94, 
     "humidity": 92, 
     "temp_kf": -1.06 
    }, 
    "weather": [ 
    { 
     "id": 801, 
     "main": "Clouds", 
     "description": "few clouds", 
     "icon": "02d" 
    }], 
    "clouds": 
    { 
     "all": 24 
    }, 
    "wind": 
    { 
     "speed": 3.2, 
     "deg": 273 
    }, 
    "sys": 
    { 
     "pod": "d" 
    }, 
    "dt_txt": "2016-02-17 21:00:00" 
}, 
{ 
    "dt": 1455753600, 
    "main": 
    { 
     "temp": 0.48, 
     "temp_min": 0.48, 
     "temp_max": 1.28, 
     "pressure": 1028.81, 
     "sea_level": 1032.42, 
     "grnd_level": 1028.81, 
     "humidity": 96, 
     "temp_kf": -0.79 
    }, 
    "weather": [ 
    { 
     "id": 801, 
     "main": "Clouds", 
     "description": "few clouds", 
     "icon": "02n" 
    }], 
    "clouds": 
    { 
     "all": 12 
    }, 
    "wind": 
    { 
     "speed": 3.76, 
     "deg": 276.002 
    }, 
    "sys": 
    { 
     "pod": "n" 
    }, 
    "dt_txt": "2016-02-18 00:00:00" 
}, 
{ 
    "dt": 1455764400, 
    "main": 
    { 
     "temp": -0.9, 
     "temp_min": -0.9, 
     "temp_max": -0.38, 
     "pressure": 1031.32, 
     "sea_level": 1034.98, 
     "grnd_level": 1031.32, 
     "humidity": 100, 
     "temp_kf": -0.53 
    }, 
    "weather": [ 
    { 
     "id": 800, 
     "main": "Clear", 
     "description": "sky is clear", 
     "icon": "01n" 
    }], 
    "clouds": 
    { 
     "all": 0 
    }, 
    "wind": 
    { 
     "speed": 3.41, 
     "deg": 300 
    }, 
    "sys": 
    { 
     "pod": "n" 
    }, 
    "dt_txt": "2016-02-18 03:00:00" 
}, 
{ 
    "dt": 1455775200, 
    "main": 
    { 
     "temp": -1.92, 
     "temp_min": -1.92, 
     "temp_max": -1.67, 
     "pressure": 1032.6, 
     "sea_level": 1036.23, 
     "grnd_level": 1032.6, 
     "humidity": 100, 
     "temp_kf": -0.26 
    }, 
    "weather": [ 
    { 
     "id": 800, 
     "main": "Clear", 
     "description": "sky is clear", 
     "icon": "01n" 
    }], 
    "clouds": 
    { 
     "all": 0 
    }, 
    "wind": 
    { 
     "speed": 3.61, 
     "deg": 289.502 
    }, 
    "sys": 
    { 
     "pod": "n" 
    }, 
    "dt_txt": "2016-02-18 06:00:00" 
}, 
{ 
    "dt": 1455786000, 
    "main": 
    { 
     "temp": -2.23, 
     "temp_min": -2.23, 
     "temp_max": -2.23, 
     "pressure": 1035.25, 
     "sea_level": 1038.94, 
     "grnd_level": 1035.25, 
     "humidity": 100, 
     "temp_kf": 0 
    }, 
    "weather": [ 
    { 
     "id": 800, 
     "main": "Clear", 
     "description": "sky is clear", 
     "icon": "01n" 
    }], 
    "clouds": 
    { 
     "all": 0 
    }, 
    "wind": 
    { 
     "speed": 4.17, 
     "deg": 322.001 
    }, 
    "sys": 
    { 
     "pod": "n" 
    }, 
    "dt_txt": "2016-02-18 09:00:00" 
}, 
{ 
    "dt": 1455796800, 
    "main": 
    { 
     "temp": -3.76, 
     "temp_min": -3.76, 
     "temp_max": -3.76, 
     "pressure": 1038.33, 
     "sea_level": 1042.06, 
     "grnd_level": 1038.33, 
     "humidity": 100, 
     "temp_kf": 0 
    }, 
    "weather": [ 
    { 
     "id": 800, 
     "main": "Clear", 
     "description": "sky is clear", 
     "icon": "01d" 
    }], 
    "clouds": 
    { 
     "all": 0 
    }, 
    "wind": 
    { 
     "speed": 3.79, 
     "deg": 330.001 
    }, 
    "sys": 
    { 
     "pod": "d" 
    }, 
    "dt_txt": "2016-02-18 12:00:00" 
}, 
{ 
    "dt": 1455807600, 
    "main": 
    { 
     "temp": -2.22, 
     "temp_min": -2.22, 
     "temp_max": -2.22, 
     "pressure": 1040.61, 
     "sea_level": 1044.24, 
     "grnd_level": 1040.61, 
     "humidity": 100, 
     "temp_kf": 0 
    }, 
    "weather": [ 
    { 
     "id": 800, 
     "main": "Clear", 
     "description": "sky is clear", 
     "icon": "01d" 
    }], 
    "clouds": 
    { 
     "all": 0 
    }, 
    "wind": 
    { 
     "speed": 3.78, 
     "deg": 341.506 
    }, 
    "sys": 
    { 
     "pod": "d" 
    }, 
    "dt_txt": "2016-02-18 15:00:00" 
}] 
} 
+0

*代碼突然停在後面的線路全部*意味着有什麼不對。請再次檢查您的logcat以進行堆棧跟蹤。另外,它在哪裏停止? – Rohit5k2

回答

0

如果我沒看錯的則OWM_DESCRIPTION值應爲description

因此,這裏是我的分析

  1. JSONObject tempratureobject = daydata.getJSONObject(OWM_DESCRIPTION);

    應該像

    SONObject tempratureobject = daydata.getJSONObject(OWM_MAIN); // use variable which has value "main" 
    
  2. JSONObject weatherobject = daydata.getJSONObject(OWM_WEATHER);

    應該

    JSONObject weatherobject = daydata.getJSONArray(OWM_WEATHER).getJSONObject(0); 
    

    因爲weather節點是一個數組,你需要這個數組的第一個對象。

+1

謝謝Rohit。通過首先訪問天氣節點的數組,然後訪問天氣節點中的對象,解決了問題。 –

+0

完美。請不要忘記接受答案。 – Rohit5k2

0
String description = weatherobject.getString(OWM_DESCRIPTION); 

    JSONObject tempratureobject = daydata.getJSONObject(OWM_DESCRIPTION); 

你這裏指的是它作爲一個字符串,並作爲objetc,它只是你的JSON中的字符串