【问题标题】:How to parse OpenWeatherMap 16-day json?如何解析 OpenWeatherMap 16 天 json?
【发布时间】:2025-11-23 22:50:01
【问题描述】:

您好,我正在尝试创建一个 Android 应用程序,该应用程序将显示来自 opneweathermap 的天气。到目前为止,我的应用程序显示了今天的天气,但我无法解析 16 天的 JSON。我正在尝试获取每一天的最低和最高温度。任何帮助将不胜感激。

{"cod":"200","message":0.4667,"cnt":7,"list":[{"dt":1512777600,"main":{"temp":0.71,"temp_min" :0.71,"temp_max":5.21,"压力":1030.25,"sea_level":1035.12,"grnd_level":1030.25,"湿度":100,"temp_kf":-4.5},"天气":[{"id" :800,"main":"Clear","description":"晴空","icon":"01n"}],"clouds":{"all":0},"wind":{"speed" :8.86,"deg":304.5},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12-09 00:00:00"},{ "dt":1512788400,"main":{"temp":1.35,"temp_min":1.35,"temp_max":4.72,"pressure":1029.09,"sea_level":1034,"grnd_level":1029.09,"湿度" :100,"temp_kf":-3.38},"weather":[{"id":800,"main":"Clear","description":"晴空","icon":"01n"}], "clouds":{"all":0},"wind":{"speed":8.07,"deg":292},"rain":{},"sys":{"pod":"n"} ,"dt_txt":"2017-12-09 03:00:00"},{"dt":1512799200,"main":{"temp":2.21,"temp_min":2.21,"temp_max":4.46,"压力":1027.47,"sea_level":1032.37,"grnd_level":1027.47,"湿度":100,"temp_kf":-2.25},"天气":[{"id":801,"main":"云" ,"description":"几朵云","icon":"02n"}],"clouds":{"all" :24},"wind":{"speed":7.66,"deg":280.001},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017- 12-09 06:00:00"},{"dt":1512810000,"main":{"temp":3.35,"temp_min":3.35,"temp_max":4.47,"pressure":1025.64,"sea_level" :1030.48,"grnd_level":1025.64,"湿度":100,"temp_kf":-1.13},"weather":[{"id":802,"main":"Clouds","description":"散落的云","icon":"03d"}],"clouds":{"all":48},"wind":{"speed":6.69,"deg":275.001},"rain":{}," sys":{"pod":"d"},"dt_txt":"2017-12-09 09:00:00"},{"dt":1512820800,"main":{"temp":5.02," temp_min":5.02,"temp_max":5.02,"pressure":1023.2,"sea_level":1028.06,"g​​rnd_level":1023.2,"humanity":100,"temp_kf":0},"weather":[{"id ":803,"main":"Clouds","description":"破云","icon":"04d"}],"clouds":{"all":68},"wind":{"speed ":6.12,"deg":266},"rain":{},"sys":{"pod":"d"},"dt_txt":"2017-12-09 12:00:00"}, {"dt":1512831600,"main":{"temp":5.29,"temp_min":5.29,"temp_max":5.29,"pressure":1019.44,"sea_level":1024.28,"grnd_level":1019.44,"湿度":100,"temp_kf":0},"天气":[{"id":802,"main":"Cl ouds","description":"散落的云朵","icon":"03d"}],"clouds":{"all":44},"wind":{"speed":4.76,"deg":266.002 },"rain":{},"sys":{"pod":"d"},"dt_txt":"2017-12-09 15:00:00"},{"dt":1512842400,"main ":{"temp":4.54,"temp_min":4.54,"temp_max":4.54,"pressure":1016.14,"sea_level":1021.07,"grnd_level":1016.14,"湿度":100,"temp_kf":0 },"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all": 0},"wind":{"speed":3.37,"deg":254.504},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12 -09 18:00:00"}],"city":{"id":2964574,"name":"Dublin","coord":{"lat":53.344,"lon":-6.2672},"国家":"IE"}}

private void renderWeather(JSONObject json)
{
    /*
    //minField.setText(json.toString());

    try
    {
        JSONArray resultArray = json.getJSONArray("list");
        for(int i = 0; i < resultArray.length(); i++)
        {
            JSONObject obj = resultArray.getJSONObject(i);
            //store your variable
            String tempMin = obj.getString("temp_min");
            Log.i("TAG","Temp Min " + i + ": "+ tempMin);
        }
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
    */

    String yourJsonString = json.toString();

    JSONArray jsonArray = null;
    try
    {
        jsonArray = new JSONArray(yourJsonString);

        for (int i = 0; i < jsonArray.length(); i++)
        {
            JSONObject obj1 = jsonArray.getJSONObject(i);
            JSONArray results = obj1.getJSONArray("list");
            String tempMin =  results.getJSONObject(0).getString("temp_min");
            Log.i("TAG","Temp Min " + i + ": "+ tempMin);
        }
    }

    catch (JSONException e)
    {
        e.printStackTrace();
    }
}

我已经尝试了代码中显示的两种方法,但都不起作用。第一种方法得到一个 JSONException: No value for temp_min 错误。第二种方式得到一个 JSONException: Value error。

【问题讨论】:

  • 注释掉的代码应该可以工作,除了 temp_min 值不是字符串值,它是一个数字。使用 obj.getDouble("temp_min") 而不是 obj.getString("temp_min");编辑:第二个代码不起作用,因为你拥有的不是 JSONArray,它是一个 JSONObject,它有一个 JSONArray 作为键“list”的值。
  • 您好,我将行更改为 Double tempMin = obj.getDouble("temp_min");它给出了一个错误:org.json.JSONException: No value for temp_min.

标签: java android json openweathermap


【解决方案1】:

每当您获得无法立即理解的 JSON 数据块时,首先尝试通过美化器(也称为格式化程序)运行它以查看结构。

我将您的 JSON 上传到这个唯一的 JSON 格式化程序中:

https://jsonformatter.curiousconcept.com

下面是结果。现在你可以开始理解它了。

看起来下面的大纲块代表一天。相应地编写代码以对其进行解析。

【讨论】:

    【解决方案2】:

    从顶部开始查看您必须使用的结构: 你有一个 JSONObject,它有 5 个字段。键“cod”保存字符串值“200”,键“message”,数值为0.4667,键“cnt”,数值为7,键“list”,其值为JSONArray , 和键“country”,其中包含字符串值“IE”。那是最外层的 JSON 对象。从中您要解析键“列表”中的 JSONArray 值。到目前为止,你很好!但是,如果您查看 JSONArray 中的每个 JSONObject,您会看到每个中的第二个键“main”,它的值是另一个 JSONObject!它变得更好了,因为“天气”包含一个 JSONArray,其中只有一个 JSONObject 用于第一个,但是作为一个数组,您可以认为它可能包含多个对象,因此您也必须遍历它。这会有点涉及,但可行,您只需仔细查看预期格式并正确识别部分即可。

    【讨论】:

      【解决方案3】:

      Please look the controller code in the question

      首先得到响应,如果肯定则反序列化,然后在java语法中使用json转换。

      【讨论】: