如何在Java中使用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"
}
]
}
]
}
答案 0 :(得分:0)
您可以使用方法JSONObject.put(String, Object)
从内部对象到外部对象,即从用户到组。 Object可以是其他JSON对象的数组,因此您可以构建层次结构(JSONObject.put(String, Collection)
也应该起作用)。
给你一个想法:
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 });