【发布时间】: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,"grnd_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