2

i am creating a simple weather application just so i can practice with Json, lets say i want to display the first 5 days within a week. The json data below only shows 2 days but the principle still applies. How can i display or specify that i want to use an object that displays the temperature at 12:00 of each day? (dt_txt containing the time)

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

So far i can access each object within a JsonArray using a for loop, using the code:

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

how can i only access the data that has a time of 12:00 only?

6
  • With time you mean dt_txt containing 12:00? Commented Sep 7, 2017 at 10:45
  • use Gson to parse json string to your POJO Commented Sep 7, 2017 at 10:54
  • @jiashie can you please how me an example? Commented Sep 7, 2017 at 11:11
  • @AshRah96 First you need to parse dt_txt:"2017-09-07 12:00:00" and then put if(check time is 12) { then do your logic } Commented Sep 7, 2017 at 11:34
  • @AshRah96 see my answer Commented Sep 7, 2017 at 11:46

2 Answers 2

1

You can do something like this.

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.