java - 为什么我没有得到 jsonObject.getString ("weather"的任何输出?

标签 java android json

 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

最佳答案

你可以调用getJSONArray,

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

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

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

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

关于java - 为什么我没有得到 jsonObject.getString ("weather"的任何输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61029419/

相关文章:

PHP 获取 JSON POST 数据

java - Spring Security、Spring LDAP、Hibernate如何处理用户信息?

java - JAX-WS - 如何禁用 @WebMethod 上的自动响应?

java - 将 TextView 设置为可见时崩溃

android - Spotify Auth 库在登录时返回 Type.EMPTY

通过 POST 获取 JSON 并吐回图像的 Php webservice

javascript - 重组 CSV 数据以创建正确的 JSON 格式

java - tomcat 服务器上的 Intellij Web 应用程序显示 http ://localhost:8080/index. jsp 而不是 http ://localhost:8080/myapp/index. jsp

java - 为什么我需要向 Java 接口(interface)添加方法,以便在 Junit 单元测试中访问它?

java - 带有 NavigationDrawer 的 fragment 中的 TabHost