3

I am trying to create a complex nested object like this:

{
   "user":{
      "type":"facebook",
      "first_name":"harsha",
      "last_name":"mv",
      "gender":"male"
   },
   "friends":[
      {
         "id":"23",
         "name":"Vachana"
      },
      {
         "id":"23",
         "name":"Jyosna"
      },
      {
         "id":"23",
         "name":"Avinash"
      }
   ]
}

Android Java code:

    JSONObject user = new JSONObject();
    try {
        user.put("first_name", "harsha");
        user.put("last_name", "mv");
        user.put("gender", "mail");

        System.out.println(user);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }

I want to nest the other objects as shown above. Can someone tell me how I can do it? What function or format do I need to use?

8 Answers 8

10

'friends' is a JSONArray, each of its items is a JSONObject. Just use put with a JSONObject or JSONArray as value. For arrays, you will have to specify the index as well.

Sign up to request clarification or add additional context in comments.

Comments

5

There is a function in JSONObject

put(String string, Object value)

that takes in a JSONArray, of which you can make it an array of JSONObject's. I'd imagine your "friend" class could generate the JSONObject that is be put into the list.

Comments

2

Checkout these JSON encoding examples, so you can manage it easily through.

JSON Encoding

Comments

1

i am switching the subject, but insist you to use Gson as it maintain a all element in one class with runtime getter/setter

Comments

1

Use this Json jar file its automatically set and get the values according to tagnames(Ex:firstname,lastname etc) download and usage of jar file get from belo link

Android json jar file

Comments

1

Gson is best suited for this, with Gson you can use regular classes of java and turn them to JSON objects , here is a sample :

public class Member
{    
public String  ID;
public String FirstName;    
public String LastName;    
public String Email;    
public String DisplayName ;  
public String DOB;    
public String AboutMe;   
public String MemberType;   
public String UserTypeName;

}

initiate Member class and fill in the values
Member member = new Member();
 member.ID = "1";
 .......

 //now use GSON to get the JSON

 Gson gson = new Gson();
 String JSONString = gson.toJson(member);

for more check this http://code.google.com/p/google-gson/

1 Comment

how is this nested?
0

if you want to make nested json objects then you can use following code :

            JSONObject objFirst = new JSONObject();
            objFirst.put("description", "ADBCD");
            objFirst.put("type", "TEXT");


            JSONObject objSecond = new JSONObject();
            objSecond.put("askjdhasjk", "XYZ");

            objFirst.accumulate("This Nw", objSecond);

Comments

0
//Import the following
//import java.util.ArrayList;
//import org.bson.Document;

Document root= new Document();
Document rootUser = new Document();
ArrayList rootFriends= new ArrayList();
Document rootFriends0 = new Document();
Document rootFriends1 = new Document();
Document rootFriends2 = new Document();




rootUser.append("type","facebook");   
rootUser.append("first_name","harsha");
rootUser.append("last_name","mv");
rootUser.append("gender","male");
rootFriends0.append("id","23");
rootFriends0.append("name","Vachana");
rootFriends1.append("id","23");
rootFriends1.append("name","Jyosna");
rootFriends2.append("id","23");
rootFriends2.append("name","Avinash");




if (!rootUser.isEmpty()){
root.append("user",rootUser);
}
if (!rootFriends.isEmpty()){
root.append("friends",rootFriends);
}
if (!rootFriends0.isEmpty()){
rootFriends.add(rootFriends0);
}
if (!rootFriends.isEmpty()){
root.append("friends",rootFriends);
}
if (!rootFriends1.isEmpty()){
rootFriends.add(rootFriends1);
}
if (!rootFriends.isEmpty()){
root.append("friends",rootFriends);
}
if (!rootFriends2.isEmpty()){
rootFriends.add(rootFriends2);
}
if (!rootFriends.isEmpty()){
root.append("friends",rootFriends);
}


System.out.println(root.toJson());

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.