-1

I am able to parse Json object but not able to parse nested JSON object.I am able to parse upto "base64" (below JSON DATA)but not able to parse then.How can the object within object can be parsed?

JSON Data

{

    "StdID":1,
    "NAME":"Kirsten Green",
    "PHONENO":"095-517-0049",
    "DOB":"2009-12-28T00:00:00",
    "CLASS":9,
    "GENDER":"M",
    "ADDRESS":"8254 At Ave",
    "NATIONALITY":"Belgium",
    "ENROLLEDYEAR":"2016-04-21T00:00:00",
    "Photo":null,
    "Cat_ID":5,
    "base64":null,
    "studentDetails":{
        "StdID":1,
        "GUARDIAN_PHONE_NO":"002-283-4824",
        "MOBILE_NO":"1-377-762-8548",
        "First_NAME":"Maile",
        "Last_Name":"Lancaster",
        "Relation":"Father",
        "DOB":"2017-02-22T00:00:00",
        "Education":"Ph.D",
        "Occupation":"Etiam ligula tortor,",
        "Income":"20000-30000",
        "Email":"[email protected]",
        "AddLine1":"Ap #416-4247 Sollicitudin Av.",
        "AddLine2":"Ap #801-7380 Imperdiet Avenue",
        "State":"ME",
        "Country":"Israel"
    },
    "Marks":null,
    "stdCategory":{
        "Cat_ID":5,
        "Category":"Normal"
    }

}

Home class

 public void makeJsonObjectRequest(int stud_id) {
        String URL = Navigation_URL + stud_id;
        Log.d("TAG", "URL:" + URL);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            studentInformation = new StudentInformation();

                            studentInformation.StdID = String.valueOf(jsonObject.get("StdID"));
                            studentInformation.Name = jsonObject.getString("NAME");
                            studentInformation.Gender = (String) jsonObject.get("GENDER");
                            studentInformation.Phonenumber = String.valueOf(jsonObject.get("PHONENO"));
                            studentInformation.StudentClass = String.valueOf(jsonObject.get("CLASS"));
                            studentInformation.Enrolled_Year = String.valueOf(jsonObject.get("ENROLLEDYEAR"));
                            studentInformation.Address = String.valueOf(jsonObject.get("ADDRESS"));
                            studentInformation.DOB = String.valueOf(jsonObject.get("DOB"));
                            studentInformation.Nationality = String.valueOf(jsonObject.get("NATIONALITY"));
                            profilename.setText(studentInformation.Name);






                        } catch (JSONException e) {
                            Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }

                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Home.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);


    }

how can nested json be parsed?

3
  • You could not find an answer anywhere on the internet? Commented Mar 8, 2017 at 9:21
  • I had similar issue yesterday, if you are using gson to parse data, you can create a model and do getter and setters. Have a look this link stackoverflow.com/questions/42652448/… Commented Mar 8, 2017 at 9:35
  • did You test my answer? Commented Mar 8, 2017 at 9:49

6 Answers 6

1

Try this,

        public void makeJsonObjectRequest(int stud_id) {
        String URL = Navigation_URL + stud_id;
        Log.d("TAG", "URL:" + URL);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            studentInformation = new StudentInformation();

                            studentInformation.StdID = String.valueOf(jsonObject.get("StdID"));
                            studentInformation.Name = jsonObject.getString("NAME");
                            studentInformation.Gender = (String) jsonObject.get("GENDER");
                            studentInformation.Phonenumber = String.valueOf(jsonObject.get("PHONENO"));
                            studentInformation.StudentClass = String.valueOf(jsonObject.get("CLASS"));
                            studentInformation.Enrolled_Year = String.valueOf(jsonObject.get("ENROLLEDYEAR"));
                            studentInformation.Address = String.valueOf(jsonObject.get("ADDRESS"));
                            studentInformation.DOB = String.valueOf(jsonObject.get("DOB"));
                            studentInformation.Nationality = String.valueOf(jsonObject.get("NATIONALITY"));
                            profilename.setText(studentInformation.Name);

                            JSONObject studentDetails_obj=jsonObject.getJSONObject("studentDetails");
                            int StdID=studentDetails_obj.getInt("StdID");
                            String GUARDIAN_PHONE_NO=studentDetails_obj.getString("GUARDIAN_PHONE_NO");
                            String MOBILE_NO=studentDetails_obj.getString("MOBILE_NO");
                            String First_NAME=studentDetails_obj.getString("First_NAME");
                            String Last_Name=studentDetails_obj.getString("Last_Name");


                            JSONObject stdCategory_obj=jsonObject.getJSONObject("stdCategory");
                            int Cat_ID=stdCategory_obj.getInt("Cat_ID");
                            String Category=stdCategory_obj.getString("Category");

                        } catch (JSONException e) {
                            Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }

                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Home.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
      }
Sign up to request clarification or add additional context in comments.

Comments

1

You need to parse the nested object like

JSONObject jsonObject = new JSONObject(response);
JSONObject studentDetail = jsonObject.getJSONObject("studentDetails");

and then you can get the values like

String.valueOf(studentDetail.get("StdID"));

Similarly you can access the other nested JSON object like above

In case your nested JSON object is an array you need to make use of getJSONArray function

JSONArray array1 = jsonObject.getJSONArray("keyAttribute");

Comments

1

try this:

 try{
    JSONObject json = new JSONObject(jsonString);  //your JSON String
    JSONObject studentDetails = json.getJSONObject("studentDetails");
    String StdID = String.valueOf(studentDetails.getString("StdID"));
    String GUARDIAN_PHONE_NO = String.valueOf( studentDetails.getString("GUARDIAN_PHONE_NO"));
    //rest of the strings
     }
     catch (JSONException e){
           e.printStackTrace();
    }

Comments

1

you are using JSON object in JSON object try this

JSONObject jsonObject = new JSONObject(response);
JSONObject jsonObject2=jsonObject.getJSONObject("studentDetails");

and try to get studentDetails from jsonObject2 and like this for "stdCategory"

5 Comments

i accept logic as JSONObject studentDetails_obj1 = jsonObject.getJSONObject("stdCategory"); studentInformation.Category = studentDetails_obj1.getString("Category"); it works fine
i have many correct ans .so when i put correct on one ,then other dispaaear
yours giving wrong. giving right value mate by following JSONObject studentDetails_obj1=jsonObject.getJSONObject("stdCategory"); studentInformation.Category = studentDetails_obj1.getString("Category");
@seon I just write down just snippet. I didn't write the whole code
thank you very much for your effort.Thanks alot @Kiran
0

See my answer How to use Google/GSON to convert a JSON string into Java POJO? it may help you to create POJO from response you not need to get value by key manualy.

Gson gson = new Gson();
POJOClass pojo = gson.fromJson(jsonObject.toString(), new TypeToken<POJOClass>() {}.getType());

Comments

0

Yes, You can parse nested json object.

if (!exists) {
        keys = json.keys();
        while (keys.hasNext()) {
            nextKeys = (String) keys.next();
            try {

                if (json.get(nextKeys) instanceof JSONObject) {

                    if (exists == false) {
                        getKey(json.getJSONObject(nextKeys), key);
                    }

                } else if (json.get(nextKeys) instanceof JSONArray) {
                    JSONArray jsonarray = json.getJSONArray(nextKeys);
                    for (int i = 0; i < jsonarray.length(); i++) {
                        String jsonarrayString = jsonarray.get(i).toString();
                        JSONObject innerJSOn = new JSONObject(jsonarrayString);

                        if (exists == false) {
                            getKey(innerJSOn, key);
                        }

                    }

                }

            } catch (Exception e) {
                // TODO: handle exception
            }

        }

For complete understanding checkout below: Code Explanation : https://www.youtube.com/watch?v=ZjZqLUGCWxo

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.