运行android代码时未创建JSONArray
我正在以JSON格式解析从url接收的响应字符串。代码停在创建JSONArray的行上。它不会打印任何日志或异常。当我尝试调试时,我可以看到正在使用正确的值创建JSONArray,但随后代码在后面的行中突然停止。代码片段是-运行android代码时未创建JSONArray,android,json,Android,Json,我正在以JSON格式解析从url接收的响应字符串。代码停在创建JSONArray的行上。它不会打印任何日志或异常。当我尝试调试时,我可以看到正在使用正确的值创建JSONArray,但随后代码在后面的行中突然停止。代码片段是- private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays) throws JSONException { JSONObject fore
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);
如果我没有错,那么
OWM_DESCRIPTION
的值应该是DESCRIPTION
下面是我的分析
JSONObject tempratureobject=daydata.getJSONObject(OWM_描述)代码>
应该是
SONObject tempratureobject = daydata.getJSONObject(OWM_MAIN); // use variable which has value "main"
JSONObject weatherobject=daydata.getJSONObject(OWM_WEATHER)代码>
应该是
JSONObject weatherobject = daydata.getJSONArray(OWM_WEATHER).getJSONObject(0);
因为weather
节点是一个数组,您需要该数组的第一个对象
在这里,您将其称为字符串和对象,其中它只是JSON中的一个字符串代码在后面的几行突然停止,表示有问题。请再次检查日志,查看堆栈跟踪。还有,它停在哪里?谢谢。通过首先访问气象节点的阵列,然后访问其中的对象来解决此问题。完美。请不要忘记接受答案。
JSONObject weatherobject = daydata.getJSONArray(OWM_WEATHER).getJSONObject(0);
String description = weatherobject.getString(OWM_DESCRIPTION);
JSONObject tempratureobject = daydata.getJSONObject(OWM_DESCRIPTION);