0

How can I recreate the following JSON object, in Javausing JSONObject?

{  
   "RequestorId":121,
   "Groups":[  
      {  
         "GroupID":1,
         "GroupName":"xyz",
         "ContentGroup":"abc",
         "Regions":"india",
         "MarketsCovered":"all",
         "Users":[  
            {  
               "UserId":101,
               "FirstName":"aaa",
               "LastName":"yyy",
               "Work_Location":"blore",
               "CurrentRole":"ccc",
               "LanguageSkills":"english"
            },
            {  
               "UserId":102,
               "FirstName":"bbb",
               "LastName":"vvv",
               "Work_Location":"blore",
               "CurrentRole":"ttt",
               "LanguageSkills":"urdu"
            }
         ]
      },
      {  
         "GroupID":2,
         "GroupName":"yyy",
         "ContentGroup":"bca",
         "Regions":"india",
         "MarketsCovered":"kkk",
         "Users":[  
            {  
               "UserId":108,
               "FirstName":"hhh",
               "LastName":"jjj",
               "Work_Location":"blore",
               "CurrentRole":"ggg",
               "LanguageSkills":"english"
            },
            {  
               "UserId":333,
               "FirstName":"rrr",
               "LastName":"eee",
               "Work_Location":"mandya",
               "CurrentRole":"ddd",
               "LanguageSkills":"english"
            }
         ]
      }
   ]
}
1

1 Answer 1

0

You can work your way from the inner objects to the outer objects, i.e. from Users to Groups, by using the method JSONObject.put(String, Object). Object can be an array of other JSON objects, so you can build up the hierarchy (JSONObject.put(String, Collection) should work as well).

To give you an idea:

    JSONObject user1 = new JSONObject().put("UserId", 101).put("FirstName", "aaa"); // Create user1
    JSONObject user2 = new JSONObject().put("UserId", 102).put("FirstName", "bbb"); // Create user2

    // Now create a group and nest the two users within the group
    JSONObject groups1 = new JSONObject().put("GroupId", 1).put("GroupName", "xyz");

    // Nest users by using an array of JSON objects
    groups1.put("Users", new JSONObject[] { user1, user2 });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.