1

How to parse JSON values in this format? I want to get the details of the data element but inside data there are 'dates' and inside dates there is array containing two more elements. I want to get all the dates first inside data and then within these dates I want all the information within these dates. How can I achieve this? Please Help. I tried with below code but it hasn't worked

try {
    JSONObject jsonObject = new JSONObject("data");
    JSONArray jsonArray =jsonObject.getJSONArray(String.valueof(cuurentdate));

    JSONArray session;
    for (int i = 0; i < jsonArray.length() - 1; i++) {
        jsonObject = jsonArray.getJSONObject(i);
        session= jsonObject.getJSONArray("session");

        Log.d("MyLog", session + "");
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Following is the format

{
  "status": 1,
  "status_code": 200,
  "data": {
    "2018-02-11": [
      {
        "session": "01:00 AM",
        "place": true

      },
      {
        "session": "02:00 AM",
        "place": true

      }
    ],
    "2018-02-12": [
      {
        "session": "01:00 AM",
        "place": true

      },
      {
        "session": "02:00 AM",
        "place": true
      }
    ]
  }
}
11
  • 1
    Invalid JSON variable. Commented Aug 30, 2018 at 10:30
  • edited, that was a typo mistake Commented Aug 30, 2018 at 10:31
  • 1
    Your json format is not valid now. Commented Aug 30, 2018 at 10:34
  • Can you please help me with correction? I am a newbie to this parsing thing Commented Aug 30, 2018 at 10:35
  • Check your json here jsonviewer.stack.hu Commented Aug 30, 2018 at 10:36

3 Answers 3

1

You just need to pass the response string to the method. You can try this:

  private void jsonParsing(String jsonString) {

   // String jsonString = "{ \"status\": 1, \"status_code\": 200, \"data\": { \"2018-02-11\": [ { \"session\": \"01:00 AM\", \"place\": true }, { \"session\": \"02:00 AM\", \"place\": true } ], \"2018-02-12\": [ { \"session\": \"01:00 AM\", \"place\": true }, { \"session\": \"02:00 AM\", \"place\": true } ] } }";
    try {
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONObject dataObj = jsonObject.getJSONObject("data");

        Iterator<String> iter = dataObj.keys();

        Log.e(TAG, "jsonParsing: "+iter );

        while (iter.hasNext()) {
            String key = iter.next();
                JSONArray datesArray = dataObj.getJSONArray(key);

                ArrayList<String> sessions = new ArrayList<String>();

                for (int i = 0; i < datesArray.length(); i++) {
                    JSONObject datesObject = datesArray.getJSONObject(i);

                    sessions.add(datesObject.getString("session"));
                }
                Log.d("MyLog", sessions + "");

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks,. let me check this
Can you please tell what to write in json string. Is it empty?
The json that you want to parse.
Parsing start from data object, there is no string on top of 'status' bracket. Sorry if i am doing something wrong
when i hit the api, then in return this json response comes which i shared. Curly brackets and then status, status code and then data thing
|
1

(1) get JSONObject of Main json

 JSONObject objMain = new JSONObject("your json string");

(2)get JSONObject of "data" from main json

 JSONObject jsonData = objMain.getJSONObject("data")

(3) get all keys (dates) from object "data"

Iterator<String> iter = jsonData.keys();

while (iter.hasNext()) {

 String key = iter.next();

try {
     JSONArray arrayDate = objData.getJSONArray(key)
     for (i = 0; i < arrayDate.length(); i++) {

           JSONObject objDate = arrayDate.getJSONObject(i)
           Log.d("#session :", "" + objDate.getString("session"))
           Log.d("#place :", "" + objDate.getBoolean("place"))
          }
 } catch (JSONException e) {
    // Something went wrong!
  }
}

1 Comment

issue was resolved, thankyou for your help. UPVOTE :)
0

try this one code

IN THIS CODE jsonMstObject IS TEMP OBJECT YOU HAVE TO USE YOUR API RESPONSE JSONobject INSTEAD OF jsonMstObject

try {
        JSONObject jsonMstObject = new JSONObject("{"status":1,"status_code":200,"data":{"2018-02-11":[{"session":"01:00 AM","place":true},{"session":"02:00 AM","place":true}],"2018-02-12":[{"session":"01:00 AM","place":true},{"session":"02:00 AM","place":true}]}}");

        JSONObject jsonObject = jsonMstObject.getJSONObject("data");
        JSONArray jsonArray =jsonObject.getJSONArray(String.valueof(cuurentdate));

        ArrayList<String> arrSession = new ArrayList<String>(); 
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = jsonArray.getJSONObject(i);
            arrSession.add(jsonObject.getString("session"));
        }

        Log.d("MyLog", arrSession + "");
    } catch (JSONException e) {
        e.printStackTrace();
    }

in this code arrSession is your session string array

Ex. you passed cuurentdate = "2018-02-11" then you recived result like

[01:00 AM, 02:00 AM]

Note: this code is worked based on your cuurentdate param. This is code for get static date array from data and create String Array.

4 Comments

Thanks, let me check this
JSON Exception - value data of type java.lang.string cannot be converted to jsonobject
@Arslan Ali ok got it use JSONObject jsonObject = jsonMstObject.getJSONObject("data"); instered of JSONObject jsonObject = new JSONObject("data");
in this jsonMstObject is your Response JSONObject

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.