Kenneth Streit
Kenneth Streit

Reputation: 1272

How to make JSON object of JSON objects

I have been working with a few JSON objects and have been keeping the outer JSON an array, but is it possible to keep the outer a JSON object and have it contain other JSON objects or arrays?

This is what I have and it is in proper form and works well:

{
"outer":[{
  "profile":{
     "image":"",
     "name":"",
     "password":"",
     "favorites":[

     ]
  },
  "friends":[
     {
        "name":"",
        "image":"",
        "number":"",
        "type":"",
        "birthday":"",
        "state":""
     }
  ]
}]
}

However, is it possible to have this:

{
"outer":{
  "profile":{
     "image":"",
     "name":"",
     "password":"",
     "favorites":[

     ]
  },
  "friends":[
     {
        "name":"",
        "image":"",
        "number":"",
        "type":"",
        "birthday":"",
        "state":""
     }
  ]
}
}

This is also in proper form, but I am having trouble adding multiple JSON objects and JSON arrays to a single JSON object in Android. Every time I have the outer JSON object, it overwrites whichever object is already in there when I add another one.

This is what I've got so far. obj1 is the profile JSON object and obj2 is the friends JSON object:

JSONObject profile = new JSONObject();
profile.put("profile", obj1);
JSONObject friends = new JSONObject();
friends.put("friends", obj2);
JSONObject outer = new JSONObject():
outer.put("outer", profile);
outer.put("outer", friends);

Upvotes: 1

Views: 4565

Answers (2)

Fernando Carvalhosa
Fernando Carvalhosa

Reputation: 1107

Here is your mistake:

JSONObject profile = new JSONObject();
profile.put("profile", obj1);
JSONObject friends = new JSONObject();
friends.put("friends", obj2);
JSONObject outer = new JSONObject():
outer.put("outer", profile); 
outer.put("outer", friends); // you are overriding the profile value you have just put

To achieve this example by using JSONObjects:

{
    "outer":
     {
        "profile":
         {
            "image":"",
             "name":"",
             "password":"",
             "favorites": [] 
         },
        "friends":
         {
             "name":"",
             "image":"",
             "number":"",
             "type":"",
             "birthday":"",
             "state":""
         } 
    }
}

You should do something like this:

JSONObject profile = new JSONObject();
profile.put("image", "");
profile.put("name", "");
profile.put("password", "");
profile.put("favorites", new JSONArray());

JSONObject friends = new JSONObject();
friends.put("name", "");
friends.put("image", "");
friends.put("number", "");
friends.put("type", "");
friends.put("byrthday", "");
friends.put("state", "");

JSONObject outer = new JSONObject():
outer.put("profile", profile);
outer.put("friends", friends);

I strongly recommend you to parse your String JSON into an JAVA object for better readability and cheaper maintenance in the future

For that, I recommend you to use an external library like GSON and use it like this:

String json; // your JSON object as a string
Gson gson = new Gson(); // initializing the library object 
YourJavaObject yourJavaObject = gson.fromJson(json, YourJavaObject.class) // parsing

public class YourJavaObject 
{
    Profile profile;
    Friends friends;
}

public class Profile
{
    String image;
    String name;
    String password;
    List<Object> favorites;
}

public class Friends
{
    String name;
    String image;
    String number;
    String type;
    String birthday;
    String state;
}

Upvotes: 0

display name
display name

Reputation: 4185

The main difference of the second JSON is that you created a List<outer> instead of just an outer object.

JSONObject profile = new JSONObject();
profile.put("image", anImage); //pseudo code
profile.put("name", aProfileName); //pseudo code
//...and so on

JSONObject friends = new JSONObject();
friends.put("name", aName);
//...and so on

JSONObject outer = new JSONObject();
outer.put("profile", profile);
outer.put("friends", friends);

JSONObject outers = new JSONObject();
outers.put("outer", outer);

Upvotes: 1

Related Questions