【问题标题】:Getting JSONObject获取 JSONObject
【发布时间】:2021-04-07 19:39:26
【问题描述】:

我正在通过搜索圆圈中的城市来制作天气预报应用程序。使用 API api.openweathermap.org。 当我检索“列表”数组并运行我的应用程序时。它显示“列表没有价值”。有人可以帮助我如何检索我搜索的圈子中的所有城市。我的 API 是http://api.openweathermap.org/data/2.5/find?lat=33.5&lon=22.5&cnt=5&appid=83e20e46727fbf09e4dc1f76a2dcce62

 try {
            content2 = weather.execute("https://api.openweathermap.org/data/2.5/weather?lat=" +
                    lat + "&lon=" + lon + "&cnt=" + countCountry + "&appid=83e20e46727fbf09e4dc1f76a2dcce62").get();
           // for (int i = 0; i < countCountry.length(); i++) {
            Log.i("contentData",content2);
  //JSON
            JSONObject jsonObject = new JSONObject(content2);
            String cityList=jsonObject.getString("list");
            JSONArray cityArray=new JSONArray(cityList);

            for (int i = 0; i < countCountry.length(); i++) {

            String cityName = jsonObject.getString("name");
             //   String cityName = jsonObject.getString("count");
            String weatherData = jsonObject.getString("weather");
            String mainTemperature = jsonObject.getString("main");
            double visibility;

                    JSONArray array = new JSONArray(weatherData);

                    String main = "";
                    String description = "";
                    String temperature = "";

                    for (int j = 0; j < array.length(); j++) {
                        JSONObject weatherPart = array.getJSONObject(j);
                        main = weatherPart.getString("main");
                        description = weatherPart.getString("description");
                    }

                    //wind cloud
                    JSONObject jsonObjectWind = jsonObject.getJSONObject("wind");
                    String wind = jsonObjectWind.getString("speed");
                    JSONObject jsonObjectCloud = jsonObject.getJSONObject("clouds");
                    String cloud = jsonObjectCloud.getString("all");

                    JSONObject jsonObjectSys = jsonObject.getJSONObject("sys");
                    String countryName = jsonObjectSys.getString("country");


                    JSONObject mainPart = new JSONObject(mainTemperature);
                    temperature = mainPart.getString("temp");

                    visibility = Double.parseDouble(jsonObject.getString("visibility"));
                    //By default visibility is in meter
                    int visibiltyInKilometer = (int) visibility / 1000;

                    Log.i("Temperature", temperature);

            /*Log.i("main",main);
            Log.i("description",description);*/

                    String resultText =
                            "City :                 " + cityName + "," + countryName +
                                    "\nMain :                     " + main +
                                    "\nDescription :        " + description +
                                    "\nTemperature :        " + temperature + "*C" +
                                    "\nVisibility :              " + visibiltyInKilometer + " KM" +
                                    "\nWind:        " + wind +
                                    "\nCloud:       " + cloud;

                    result.setText(resultText);
                    //Now we will show this result on screen
               }
        }

【问题讨论】:

    标签: json android-studio


    【解决方案1】:

    代码中提到的list不是字符串。很少有其他地方可以互换使用字符串和数组。

    下面我添加了关于如何读取给定 JSON 数据的代码要点。

    JSONObject jsonObject = new JSONObject(content2);
    JSONArray list = jsonObject.getJSONArray("list");
    
    for (int i = 0; i < list.length(); i++) {
      JSONObject listObject = list.getJSONObject(i);
      String name = listObject.getString("name");
      
      JSONObject listObjectCoord = list.getJSONObject("coord");
      String lat = listObjectCoord.getString("lat");
      String lon = listObjectCoord.getString("lon");
      
      JSONObject listObjectMain = list.getJSONObject("main");
      String temp = listObjectMain.getString("temp");
      String humidity = listObjectMain.getString("humidity");
      
      JSONArray listArrayWeather = list.getJSONArrayt("weather");
      JSONObject listArrayWeatherObject = list.getJSONObject(0);
      String main = listArrayWeatherObject.getString("main");
      String description = listArrayWeatherObject.getString("description");
    }
    

    由于它只是一个要点,我没有添加代码来检索您需要的所有数据,但我相信您会理解并自己编写代码以获得所需的值。

    您可以参考this 以更好地了解如何读取 JSON 数据。

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      相关资源
      最近更新 更多