2017-09-07 15 views
1

我正在创建一个简单的天气应用程序,所以我可以用Json练习,可以说我想在一周内显示前5天。下面的json数据只显示2天,但原理仍然适用。我如何显示或指定我想要使用显示每天中午12点的温度的对象? (含有dt_txt的时间)如何在JsonArray中选择特定的JsonObject?

{ 
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" 
    } 

到目前为止我可以使用访问JsonArray内的每个对象一个for循环中,使用的代码:

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?

+0

随着时间的推移,你的意思是包含12:00'dt_txt'? –

+0

使用Gson来解析json字符串到你的POJO – jiashie

+0

@jiashie你可以请我怎么样? – AshRah96

回答

0

试试这个:

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"); 

    double dub = temp.getDouble("temp"); 

    // "dt_txt" is at the top level of the node 
    String time = Info.getString("dt_txt"); 

    //geteverything after the last space 
    String sub = time.substring(time.lastIndexOf(' ') + 1); 

    //test for 12:00:00 on time string 
    // only put in currents if it is 12:00:00 

    if(sub.equals("12:00:00") { 
     currents.setTemp(dub)); 
    } 
0

你可以做这样的事情。

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(); 
}