如何解析OpenWeatherMap 16天json?
How to parse OpenWeatherMap 16-day json?
您好,我正在尝试创建一个 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,"pressure":1030.25,"sea_level":1035.12,"grnd_level":1030.25,"humidity":100,"temp_kf":-4.5},"weather":[{"id":800,"main":"Clear","description":"clear sky","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,"humidity":100,"temp_kf":-3.38},"weather":[{"id":800,"main":"Clear","description":"clear sky","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,"pressure":1027.47,"sea_level":1032.37,"grnd_level":1027.47,"humidity":100,"temp_kf":-2.25},"weather":[{"id":801,"main":"Clouds","description":"few clouds","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,"humidity":100,"temp_kf":-1.13},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","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,"humidity":100,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","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,"humidity":100,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","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,"humidity":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},"country":"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.
从顶部开始查看您必须使用的结构:
您有一个包含 5 个字段的 JSON 对象。包含字符串值“200”的键 "cod",数值为 0.4667 的键 "message",数值为 7 的键 "cnt",键 "list"它的值是一个 JSON 数组,键 "country" 包含一个字符串值 "IE"。那是最外层的 JSON 对象。从那里你想从键 "list" 解析 JSON 数组值。到目前为止,你很好!但是,如果您查看 JSON 数组中的每个 JSON 对象,您会看到每个对象中的第二个键 "main",它的值是另一个 JSON对象!它变得更好,因为 "weather" 持有一个 JSON 数组,其中第一个只有一个 JSON 对象,但是作为一个数组,你可以认为它可以容纳多个对象,所以你也必须遍历它。这会有点复杂,但是是可行的,您只需要仔细查看预期格式并正确识别部分即可。
每当您收到无法立即理解的 JSON 数据块时,请先尝试通过美化程序(也称为格式化程序)运行 来查看其结构。
我将你的 JSON 上传到了这个 JSON 格式器中:
https://jsonformatter.curiousconcept.com
以下是结果。现在您可以开始理解它了。
看起来下面的轮廓块代表一天。相应地编写代码以对其进行解析。
Please look the controller code in the question
首先获取响应,如果是正则反序列化,然后在java语法中使用json转换。
您好,我正在尝试创建一个 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,"pressure":1030.25,"sea_level":1035.12,"grnd_level":1030.25,"humidity":100,"temp_kf":-4.5},"weather":[{"id":800,"main":"Clear","description":"clear sky","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,"humidity":100,"temp_kf":-3.38},"weather":[{"id":800,"main":"Clear","description":"clear sky","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,"pressure":1027.47,"sea_level":1032.37,"grnd_level":1027.47,"humidity":100,"temp_kf":-2.25},"weather":[{"id":801,"main":"Clouds","description":"few clouds","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,"humidity":100,"temp_kf":-1.13},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","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,"humidity":100,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","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,"humidity":100,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","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,"humidity":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},"country":"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.
从顶部开始查看您必须使用的结构: 您有一个包含 5 个字段的 JSON 对象。包含字符串值“200”的键 "cod",数值为 0.4667 的键 "message",数值为 7 的键 "cnt",键 "list"它的值是一个 JSON 数组,键 "country" 包含一个字符串值 "IE"。那是最外层的 JSON 对象。从那里你想从键 "list" 解析 JSON 数组值。到目前为止,你很好!但是,如果您查看 JSON 数组中的每个 JSON 对象,您会看到每个对象中的第二个键 "main",它的值是另一个 JSON对象!它变得更好,因为 "weather" 持有一个 JSON 数组,其中第一个只有一个 JSON 对象,但是作为一个数组,你可以认为它可以容纳多个对象,所以你也必须遍历它。这会有点复杂,但是是可行的,您只需要仔细查看预期格式并正确识别部分即可。
每当您收到无法立即理解的 JSON 数据块时,请先尝试通过美化程序(也称为格式化程序)运行 来查看其结构。
我将你的 JSON 上传到了这个 JSON 格式器中:
https://jsonformatter.curiousconcept.com
以下是结果。现在您可以开始理解它了。
看起来下面的轮廓块代表一天。相应地编写代码以对其进行解析。
Please look the controller code in the question
首先获取响应,如果是正则反序列化,然后在java语法中使用json转换。