Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行android代码时未创建JSONArray_Android_Json - Fatal编程技术网

运行android代码时未创建JSONArray

运行android代码时未创建JSONArray,android,json,Android,Json,我正在以JSON格式解析从url接收的响应字符串。代码停在创建JSONArray的行上。它不会打印任何日志或异常。当我尝试调试时,我可以看到正在使用正确的值创建JSONArray,但随后代码在后面的行中突然停止。代码片段是- private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays) throws JSONException { JSONObject fore

我正在以JSON格式解析从url接收的响应字符串。代码停在创建JSONArray的行上。它不会打印任何日志或异常。当我尝试调试时,我可以看到正在使用正确的值创建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);

如果我没有错,那么
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);