为什么我没有得到任何关于 jsonObject.getString(“weather”) 的输出?

huangapple go评论82阅读模式
英文:

Why I am not getting any output for jsonObject.getString("weather")?

问题

protected void onPostExecute(String s) {
    super.onPostExecute(s);
    log.i("URL", s); // I am getting output for this part
    try {
        JSONObject jsonObject = new JSONObject(s); // JSONObject Created
        String weatherInfo = jsonObject.getString("weather"); // Fetching info from weather section

        Log.i("JSON", urlResult); // But not for this part

        JSONArray jsonArray = new JSONArray(weatherInfo);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonPart = jsonArray.getJSONObject(i);

            // Not getting output for this two lines also
            Log.i("main", jsonPart.getString("main")); // fetching info from 'main' section
            Log.i("description", jsonPart.getString("description"));  // fetching info from 'description' section
        }
    } catch (Exception e) {
        e.printStackTrace(); // Handling Error
    }
}

我正在尝试从 openworldmap.org 获取天气信息。我正在使用 Android Studio 3.0。我将以下 URL 传递到代码中:https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22

英文:
 protected void onPostExecute(String s)
        {
            super.onPostExecute(s);
            log.i(&quot;URL&quot; , s) // I am getting output for this part
            try
                {
                    JSONObject jsonObject = new JSONObject(s); // JSONObject Created
                    String weatherInfo = jsonObject.getString(&quot;weather&quot;); // Fetching info from weather section

                    Log.i(&quot;JSON&quot;, urlResult); // But not for this part

                    JSONArray jsonArray = new JSONArray(weatherInfo);
                    for(int i = 0; i &lt; jsonArray.length(); i++)
                    {
                        JSONObject jsonPart = jsonArray.getJSONObject(i);

                        // Not getting output for this two lines also
                        Log.i(&quot;main&quot;, jsonPart.getString(&quot;main&quot;)); // fetching info from &#39;main&#39; section
                        Log.i(&quot;description&quot;, jsonPart.getString(&quot;description&quot;));  // fetching info from &#39;description&#39; section
                    }
                }
            catch (Exception e)
                {
                    e.printStackTrace(); // Handling Error
                }
        }

I am trying to fetch weather info from openworldmap.org. I am using Android Studio 3.0. URL that I am passing into code is https://samples.openweathermap.org/data/2.5/weather?q=London,uk&amp;appid=b6907d289e10d714a6e88b30761fae22

答案1

得分: 1

你可以调用 getJSONArray,

//String weatherInfo = jsonObject.getString("weather"); // 从天气部分获取信息

//Log.i("JSON", urlResult); // 但不适用于此部分
//JSONArray jsonArray = new JSONArray(weatherInfo);
JSONArray jsonArray = jsonObject.getJSONArray("weather");

并且在 for 循环中,你应该使用 get(i);

JSONObject jsonPart = jsonArray.get(i); //jsonArray.getJSONObject(i);
英文:

You can call getJSONArray,

//String weatherInfo = jsonObject.getString(&quot;weather&quot;); // Fetching info from weather section

//Log.i(&quot;JSON&quot;, urlResult); // But not for this part
//JSONArray jsonArray = new JSONArray(weatherInfo);
JSONArray jsonArray = jsonObject.getJSONArray(&quot;weather&quot;);

and in the for loop you should use get(i);

JSONObject jsonPart = jsonArray.get(i); //jsonArray.getJSONObject(i);

huangapple
  • 本文由 发表于 2020年4月4日 22:14:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61029419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定