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"
            }
         ]
      }
   ]
}
TryinHard
  • 4,078
  • 3
  • 28
  • 54
  • 3
    this might help: http://stackoverflow.com/questions/1957406/generate-java-class-from-json – ry8806 Aug 20 '15 at 08:05

1 Answers1

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 });
beosign
  • 433
  • 5
  • 10