1

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);
1
  • How are you adding those objects? Commented Jan 30, 2015 at 2:21

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, he created an object outer with another object outer inside (which is overriden in the second outer.put("outer")). I don't think JSONArray even have a put method (he would have to use add instead)
0

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;
}

7 Comments

I know that works, that's what I was saying. I'm just not sure how to implement it in code. If I have obj1 and obj2 that are both JSON objects, I can then make an outer one. I say 'outer.put("profile", obj1) and outer.put("friends", obj2)', but then when I look at it, all that is there is the "friends" object, since it overrode the profile one. How do I get around this?
Oh, now i got it. Could you show us how you are trying to do it? I would recommend you to parse your JSON object to an JAVA object and then do your thing
You are putting your JSON objects in the same variable "outer", thats why it's overriden
How would I be able to write this to internal memory? I'm using JSON form so that I can easily write it to memory and store it.
You mean persisting the JAVA Object into your app database in the phone or Shared Preferences?
|

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.