0

I need your help in parsing nested JSON Object.

Attached the JSON data:

{
  "31": {
    "basic": {
      "node_id": "31",
      "title": "test",
      "alias": "test",
      "description": "test",
      "site_id": "151336557",
      "node_type": "7",
      "privacy": "7",
      "deleted": "0",
      "status": "1",
      "created_date": "1379169518",
      "updated_date": "1379169518",
      "created_by": "140513626519686828",
      "updated_by": null,
      "readable_date": "14th Sep, 2013"
    },
    "meta": {
      "forum_id": "61"
    },
    "comments": {
      "count": 1
    },
    "likes": {
      "count": 0
    },
    "tags": [],
    "node_id": "31"
  },
  "32": {
    "basic": {
      "node_id": "32",
      "title": "testing discussion",
      "alias": "testing-discussion",
      "description": "testing",
      "site_id": "151336557",
      "node_type": "7",
      "privacy": "7",
      "deleted": "0",
      "status": "1",
      "created_date": "1379493816",
      "updated_date": "1379493816",
      "created_by": "140513795022034166",
      "updated_by": null,
      "readable_date": "18th Sep, 2013"
    },
    "meta": {
      "forum_id": "65"
    },
    "comments": {
      "count": 1
    },
    "likes": {
      "count": 0
    },
    "tags": [],
    "node_id": "32"
  }
}

Attaching the Java Code:

private void makeJsonObjectRequest() {
     showpDialog();

     JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,            urlJsonObj, null, new Response.Listener<JSONObject>() {

         @Override
         public void onResponse(JSONObject response) {
             Log.d(TAG, response.toString());

             JSONObject phone = response.getJSONObject("31").getJSONObject("basic");
             String name = phone.getString("title");
             String email = phone.getString("description");
             JSONObject comments = response.getJSONObject("31").getJSONObject("comments");
             String home = comments.getString("count");
             JSONObject like = response.getJSONObject("31").getJSONObject("likes");
             String mobile = like.getString("count");

             jsonResponse = "";
             jsonResponse += "Name: " + name + "\n\n";
             jsonResponse += "Email: " + email + "\n\n";
             jsonResponse += "Home: " + home + "\n\n";
             jsonResponse += "Mobile: " + mobile + "\n\n\n";

             txtResponse.setText(jsonResponse);

I need to retrieve all the objects but here i am retrieving only one object(I meant all node_ids). I need your suggestions . Thanks.

1
  • Use this site jsonschema2pojo.org, It helps you in converting from Json to Object Commented Oct 10, 2014 at 11:45

2 Answers 2

1

You get the entry with key "31" (response.getJSONObject("31")), however, you should iterate all keys:

 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, urlJsonObj, null, new Response.Listener<JSONObject>() {
     @Override
     public void onResponse(JSONObject response) {
         for (String key : response.keySet()) {
             JSONObject entry = response.getJSONObject(key);
             Log.d(TAG, entry.toString());

             JSONObject phone = entry.getJSONObject("basic");
             String name = phone.getString("title");
             String email = phone.getString("description");
             JSONObject comments = entry.getJSONObject("comments");
             String home = comments.getString("count");
             JSONObject like = entry.getJSONObject("likes");
             String mobile = like.getString("count");

             jsonResponse = "";
             jsonResponse += "Name: " + name + "\n\n";
             jsonResponse += "Email: " + email + "\n\n";
             jsonResponse += "Home: " + home + "\n\n";
             jsonResponse += "Mobile: " + mobile + "\n\n\n";

             txtResponse.setText(txtResponse.getText() + "\n\n" + jsonResponse); //get the old text and add it to it
         }
     } 
}

EDIT: you state you want only the node_id's? That's possible too:

 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, urlJsonObj, null, new Response.Listener<JSONObject>() {
     @Override
     public void onResponse(JSONObject response) {
         List<String> nodeIds = new ArrayList<String>();
         for (String key : response.keySet()) {
             JSONObject entry = response.getJSONObject(key);
             nodeIds.add(enty.getJSONObject("basic").getString("node_id"));
         }
         txtResponse.setText(ListUtils.toString(nodeIds)); //from apaches commons library
     }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for ur reply . I tried your code but I get this error "Can only iterate over an array or an instance of java.lang.Iterable" for response.keys()." can u suggest?
@srikanth Make keys() keySet()
@gogu from what package do you import JSONObject?
0

on this part

response.getJSONObject("31")

you need to change 31 for a variable and call this to every field of your object

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.