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

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

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

问题

  1. protected void onPostExecute(String s) {
  2. super.onPostExecute(s);
  3. log.i("URL", s); // I am getting output for this part
  4. try {
  5. JSONObject jsonObject = new JSONObject(s); // JSONObject Created
  6. String weatherInfo = jsonObject.getString("weather"); // Fetching info from weather section
  7. Log.i("JSON", urlResult); // But not for this part
  8. JSONArray jsonArray = new JSONArray(weatherInfo);
  9. for (int i = 0; i < jsonArray.length(); i++) {
  10. JSONObject jsonPart = jsonArray.getJSONObject(i);
  11. // Not getting output for this two lines also
  12. Log.i("main", jsonPart.getString("main")); // fetching info from 'main' section
  13. Log.i("description", jsonPart.getString("description")); // fetching info from 'description' section
  14. }
  15. } catch (Exception e) {
  16. e.printStackTrace(); // Handling Error
  17. }
  18. }

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

英文:
  1. protected void onPostExecute(String s)
  2. {
  3. super.onPostExecute(s);
  4. log.i(&quot;URL&quot; , s) // I am getting output for this part
  5. try
  6. {
  7. JSONObject jsonObject = new JSONObject(s); // JSONObject Created
  8. String weatherInfo = jsonObject.getString(&quot;weather&quot;); // Fetching info from weather section
  9. Log.i(&quot;JSON&quot;, urlResult); // But not for this part
  10. JSONArray jsonArray = new JSONArray(weatherInfo);
  11. for(int i = 0; i &lt; jsonArray.length(); i++)
  12. {
  13. JSONObject jsonPart = jsonArray.getJSONObject(i);
  14. // Not getting output for this two lines also
  15. Log.i(&quot;main&quot;, jsonPart.getString(&quot;main&quot;)); // fetching info from &#39;main&#39; section
  16. Log.i(&quot;description&quot;, jsonPart.getString(&quot;description&quot;)); // fetching info from &#39;description&#39; section
  17. }
  18. }
  19. catch (Exception e)
  20. {
  21. e.printStackTrace(); // Handling Error
  22. }
  23. }

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,

  1. //String weatherInfo = jsonObject.getString("weather"); // 从天气部分获取信息
  2. //Log.i("JSON", urlResult); // 但不适用于此部分
  3. //JSONArray jsonArray = new JSONArray(weatherInfo);
  4. JSONArray jsonArray = jsonObject.getJSONArray("weather");

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

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

You can call getJSONArray,

  1. //String weatherInfo = jsonObject.getString(&quot;weather&quot;); // Fetching info from weather section
  2. //Log.i(&quot;JSON&quot;, urlResult); // But not for this part
  3. //JSONArray jsonArray = new JSONArray(weatherInfo);
  4. JSONArray jsonArray = jsonObject.getJSONArray(&quot;weather&quot;);

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

  1. 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:

确定