Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am creating json string but unable to give tag to JSONObject in nested json string. Here is what I want

"User": [
        {
            "User1": {
                "name": "name1",
                "Address": "add1",
                "user_detail": {
                    "Qualification": B.E,
                    "DOB": 11/2/1990,
                }
            }
        },
        {
            "User2": {
              "name": "name2",
                "Address": "add2",
                "user_detail": {
                    "Qualification": B.E,
                    "DOB": 11/2/1990,
                }
                }

            }
        }
 ]

And I have managed to get until here

 {"User":[{"name":"name1","Address":"Add1"}, {"Qualification": "B.E", "DOB":"11/12/1990"}]}
But I am failed to add tag for JSONObject both for USer and user_details

Here is my code

                     try {
                     user = new JSONObject();
                     user.put("name", "name1");
                     user.put("Address", "B.E");
                 } catch (JSONException je) {
                     je.printStackTrace();
                 }
                 try {
                     userObj = new JSONObject();
                     userObj.put("User1", user);
                     jsonArray = new JSONArray();
                     jsonArray.put(user);

                 } catch (JSONException j) {
                     j.printStackTrace();
                 }

             }
         try {
             users = new JSONObject();
             users.put("User", jsonArray);
         } catch (JSONException e) {
             e.printStackTrace();
         }

The main thing is I am do not know, how to give tags to JSONObject.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

You could just pass the desired JSON String to the JSONObject constructor to do the job. Take a look here

share|improve this answer
    
I tried with user = new JSONObject("user1"); but did not work out... –  JNI_OnLoad Feb 7 at 8:23
    
Just pass the whole JSON string you mentioned in the question into a String object and pass it to the JSONObject(String string). In your case, 'user1' should be the String object containing the whole JSON string. –  AniDroid Feb 7 at 8:26
    
To the extend I managing it through this also ... jsonArray.put(userObj); jsonArray.put(user); seems like it is working ...testing it –  JNI_OnLoad Feb 7 at 8:31
    
I solved it ...thanks... –  JNI_OnLoad Feb 7 at 8:42

Current String Contain JSONArray of JSONObject's instead of JSONObject as root element. You can create Current Json String in java as:

 JSONArray jsonArray = new JSONArray();

// User1 JSONObjects
 user = new JSONObject();
 user.put("name", "name1");
 user.put("Address", "B.E"); 
 user_obj_one = new JSONObject();
 user_obj_one.put("User1",user);

//...same for User2...
...
 user_obj_two = new JSONObject();
 user_obj_two.put("User2",user_two);

//put in final array :
  jsonArray.put(user_obj_one);
  jsonArray.put(user_obj_two);
//....
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.