-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":"urna@sed.ca",
        "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?

Suman
  • 43
  • 11
seon
  • 1,050
  • 2
  • 13
  • 27

6 Answers6

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"

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
  • i accept logic as JSONObject studentDetails_obj1 = jsonObject.getJSONObject("stdCategory"); studentInformation.Category = studentDetails_obj1.getString("Category"); it works fine – seon Mar 08 '17 at 09:50
  • i have many correct ans .so when i put correct on one ,then other dispaaear – seon Mar 08 '17 at 09:52
  • yours giving wrong. giving right value mate by following JSONObject studentDetails_obj1=jsonObject.getJSONObject("stdCategory"); studentInformation.Category = studentDetails_obj1.getString("Category"); – seon Mar 08 '17 at 09:58
  • @seon I just write down just snippet. I didn't write the whole code – Kiran Benny Joseph Mar 08 '17 at 10:49
  • thank you very much for your effort.Thanks alot @Kiran – seon Mar 08 '17 at 11:42
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");
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
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();
    }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
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);
      }
Komal12
  • 3,340
  • 4
  • 16
  • 25
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());
Community
  • 1
  • 1
Rajesh
  • 2,618
  • 19
  • 25
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