我正在创建一个简单的天气应用程序,这样我就可以使用 Json 进行练习,假设我想显示一周内的前 5 天。下面的json数据只显示了2天,但原理仍然适用。我如何显示或指定我想要使用一个显示每天 12:00 温度的对象? (dt_txt包含时间)
{
cod:"200",
message:0.0074,
cnt:40,
list:[
{
dt:1504785600,
main:{
temp:291.34,
temp_min:291.34,
temp_max:291.648,
pressure:1012.84,
sea_level:1025.43,
grnd_level:1012.84,
humidity:69,
temp_kf:-0.31
},
weather:[
{
id:801,
main:"Clouds",
description:"few clouds",
icon:"02d"
}
],
clouds:{
all:20
},
wind:{
speed:6.06,
deg:242.502
},
sys:{
pod:"d"
},
dt_txt:"2017-09-07 12:00:00"
},
{
dt:1504796400,
main:{
temp:288.71,
temp_min:288.71,
temp_max:288.942,
pressure:1010.63,
sea_level:1023.18,
grnd_level:1010.63,
humidity:93,
temp_kf:-0.23
},
weather:[
{
id:500,
main:"Rain",
description:"light rain",
icon:"10d"
}
],
clouds:{
all:92
},
wind:{
speed:7.02,
deg:236.001
},
rain:{
3 h:1.055
},
sys:{
pod:"d"
},
dt_txt:"2017-09-07 15:00:00"
},
{
dt:1504807200,
main:{
temp:288.25,
temp_min:288.25,
temp_max:288.405,
pressure:1008.34,
sea_level:1020.83,
grnd_level:1008.34,
humidity:96,
temp_kf:-0.15
},
weather:[
{
id:500,
main:"Rain",
description:"light rain",
icon:"10d"
}
],
clouds:{
all:92
},
wind:{
speed:6.83,
deg:235.004
},
rain:{
3 h:1.505
},
sys:{
pod:"d"
},
dt_txt:"2017-09-07 18:00:00"
},
{
dt:1504818000,
main:{
temp:287.77,
temp_min:287.77,
temp_max:287.846,
pressure:1007.04,
sea_level:1019.52,
grnd_level:1007.04,
humidity:92,
temp_kf:-0.08
},
weather:[
{
id:500,
main:"Rain",
description:"light rain",
icon:"10n"
}
],
clouds:{
all:92
},
wind:{
speed:6.33,
deg:243
},
rain:{
3 h:0.025
},
sys:{
pod:"n"
},
dt_txt:"2017-09-07 21:00:00"
},
{
dt:1504828800,
main:{
temp:287.447,
temp_min:287.447,
temp_max:287.447,
pressure:1004.6,
sea_level:1017.03,
grnd_level:1004.6,
humidity:94,
temp_kf:0
},
weather:[
{
id:500,
main:"Rain",
description:"light rain",
icon:"10n"
}
],
clouds:{
all:92
},
wind:{
speed:7,
deg:241.004
},
rain:{
3 h:0.285
},
sys:{
pod:"n"
},
dt_txt:"2017-09-08 00:00:00"
},
{
dt:1504839600,
main:{
temp:287.117,
temp_min:287.117,
temp_max:287.117,
pressure:1002.19,
sea_level:1014.65,
grnd_level:1002.19,
humidity:97,
temp_kf:0
},
weather:[
{
id:500,
main:"Rain",
description:"light rain",
icon:"10n"
}
],
clouds:{
all:92
},
wind:{
speed:6.17,
deg:252.002
},
rain:{
3 h:1.265
},
sys:{
pod:"n"
},
dt_txt:"2017-09-08 03:00:00"
},
{
dt:1504850400,
main:{
temp:286.676,
temp_min:286.676,
temp_max:286.676,
pressure:1001.27,
sea_level:1013.77,
grnd_level:1001.27,
humidity:94,
temp_kf:0
},
weather:[
{
id:500,
main:"Rain",
description:"light rain",
icon:"10d"
}
],
clouds:{
all:88
},
wind:{
speed:5.71,
deg:261.514
},
rain:{
3 h:0.085
},
sys:{
pod:"d"
},
dt_txt:"2017-09-08 06:00:00"
},
{
dt:1504861200,
main:{
temp:288.297,
temp_min:288.297,
temp_max:288.297,
pressure:1000.5,
sea_level:1012.88,
grnd_level:1000.5,
humidity:86,
temp_kf:0
},
weather:[
{
id:804,
main:"Clouds",
description:"overcast clouds",
icon:"04d"
}
],
clouds:{
all:88
},
wind:{
speed:5.92,
deg:255.502
},
rain:{
},
sys:{
pod:"d"
},
dt_txt:"2017-09-08 09:00:00"
},
{
dt:1504872000,
main:{
temp:287,
temp_min:287,
temp_max:287,
pressure:998.66,
sea_level:1011.06,
grnd_level:998.66,
humidity:96,
temp_kf:0
},
weather:[
{
id:500,
main:"Rain",
description:"light rain",
icon:"10d"
}
],
clouds:{
all:92
},
wind:{
speed:5.74,
deg:252.501
},
rain:{
3 h:1.78
},
sys:{
pod:"d"
},
dt_txt:"2017-09-08 12:00:00"
}
到目前为止,我可以使用 for 循环访问 JsonArray 中的每个对象,使用代码:
JSONObject forecast = new JSONObject(jsonData);
JSONArray list = currentForecast.getJSONArray("list");
Current currents = new Current();
for(int i = 0; i < list.length(); i++){
JSONObject Info = list.getJSONObject(i);
JSONObject temp = Info.getJSONObject("main");
currents.setTemp(temp.getDouble("temp"));
}
如何才能只访问时间为 12:00 的数据?
最佳答案
你可以做这样的事情。
JSONObject forecast = new JSONObject(jsonData);
JSONArray list = currentForecast.getJSONArray("list");
try
{
Current currents = new Current();
for (int i = 0; i < list.length(); i++)
{
JSONObject Info = list.getJSONObject(i);
// Note: I just mentioned the way how to do logic the below .equal condition not
working when you just copy and paste the code it is just for your
reference.
// If time is 12:00
if (Info.getString("dt_txt").equals("12:00:00"))
{
// Do your logic here.
JSONObject temp = Info.getJSONObject("main");
currents.setTemp(temp.getDouble("temp"));
}
else
{
// If time is not 12:00.
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
关于java - 如何在 JsonArray 中选择特定的 JsonObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46094328/