1

I try to parse the response and try to get the Date object from the response but unable to get it. Could anyone tell me how can i get the Date object.

{
   "flag":"success",
   "msg":[
      {
         "2018-10-01":{
            "date":"2018-10-01",
            "login_time":"1538393123",
            "logout_time":"",
            "logout_message":"",
            "lock_time":"1538393236,1538393671,1538393764",
            "unlock_message":"testing,testing,break time",
            "unlock_time":"1538393363,1538393680,1538395633"
         }
      },
      {
         "2018-10-03":{
            "date":"2018-10-03",
            "login_time":"1538548533",
            "logout_time":"",
            "logout_message":"",
            "lock_time":"1538560561,1538561016,1538561260,1538561881",
            "unlock_message":"hey,gggg,gggg5555,fd",
            "unlock_time":"1538560617,1538561100,1538561273,1538566017"
         }
      }
   ]
}
ZarNi Myo Sett Win
  • 1,353
  • 5
  • 15
  • 37
  • 1
    `try to get the Date object` -- Update code what you have tried wil help others to rectify.. – Mohamed Mohaideen AH Oct 04 '18 at 07:50
  • Possible duplicate of [How do I parse JSON in Android?](https://stackoverflow.com/questions/9605913/how-do-i-parse-json-in-android) – Mohamed Mohaideen AH Oct 04 '18 at 08:45
  • @MohamedMohaideenAH How do i get that object..that is i am asking – Rishikesh Rahi Oct 04 '18 at 08:48
  • @MohamedMohaideenAH yet i have followed:- JSONObject jo_res = new JSONObject(response); if (jo_res.optString("flag").equalsIgnoreCase("success")) { JSONArray jsonArray = jo_res.getJSONArray("msg"); GlobalVariable.timeTrackerModels.clear(); for (int i = 0; i < jsonArray.length(); i++) {JSONObject jsonObject = jsonArray.getJSONObject(i); TimeTrackerModel timeTrackerModel = new TimeTrackerModel(jsonObject.getString("date"), jsonObject.getString("login_time"), – Rishikesh Rahi Oct 04 '18 at 08:54
  • Check my answer will help you..! – Mohamed Mohaideen AH Oct 04 '18 at 09:27

3 Answers3

1

Try this

 try {
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONArray jsonArray = jsonObject.getJSONArray("msg");

        for(int i=0;i < jsonArray.length();i++)
        {
            JSONObject obj = jsonArray.getJSONObject(i);
            Iterator<?> keys = obj.keys();
            while( keys.hasNext() ) {
                String key = (String)keys.next();
                if(obj.get(key) instanceof JSONObject) {
                    JSONObject dateObj = (JSONObject) obj.get(key);
                    String DATE = dateObj.getString("date");
                    Log.d("DATE",DATE);
                }
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24
0

Here's a small demo code. Please reuse the SimpleDateFormat if you're using it more often.

Date date = new SimpleDateFormat("yyyy-MM-dd").parse(object.optJSONArray("msg").optJSONObject(i).optString("date","").getTime();
Antonio Vlasic
  • 337
  • 3
  • 15
0
var object = {"flag":"success","msg":[{"2018-10-01":{"date":"2018-10-01","login_time":"1538393123","logout_time":"","logout_message":"","lock_time":"1538393236,1538393671,1538393764","unlock_message":"testing,testing,break time","unlock_time":"1538393363,1538393680,1538395633"}},{"2018-10-03":{"date":"2018-10-03","login_time":"1538548533","logout_time":"","logout_message":"","lock_time":"1538560561,1538561016,1538561260,1538561881","unlock_message":"hey,gggg,gggg5555,fd","unlock_time":"1538560617,1538561100,1538561273,1538566017"}}]};

var msg = object.msg;

var dateobjects = []; // to store the date objects

//to go through the msg array items
for(var i = 0; i < msg.length; i++)
{
    let current = msg[i];

    for(var prop in current)  
        dateobjects.push(current[prop]);
}

// Print date objects in console
console.log(dateobjects);
Jason Dar
  • 41
  • 4