简体   繁体   中英

creating a web API for JSON GET Request, how can i create JSON Objects/array for this Pattern

suggest me how to work on this, i am having all the data in linkedlinkHashmap like key pair values. i want the output in this format i am trying this but couldn't get exact format

{
RequestorId:123
UserId:111
FirstName:john
LastName:peter
Phone_Number:xxx
Email_Address:@com
Address:yyy
Picture:eeee
Work_Location:rrrr
CurrentRole:bca
LanguageSkills:english 
Groups: [
  {
     GroupID: 1
 GroupName:1
 ContentGroup:f&r
 Owner:[
    {
       UserId:111
       FirstName:eee
       LastName:rrr
    }
 ]
  }
  {
     GroupID: 2
 GroupName:2
 ContentGroup:bca
 Owner:[
    {
       UserId:121
       FirstName:www
       LastName:qqq
    },
    {
       UserId:123
       FirstName:ttt
       LastName:uuu
    }
 ]
  }
}

You could look for a java json library to convert your java data object to json such as gson library eg http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

Your example here to converting list of objects to json is similar to the solution offered in this stackoverflow post Parse List of JSON objects using GSON Where mention importantly match java model to json model via TypeAdapter

You can create a Map <String,Object> valueMap and should have the following classes

public Owner{
 int UserId;
 String FirstName;
 String LastName;
}

public Groups{
List<Owner> owner;
int GroupId;
int GroupName;
String ContentGroup;
}

and assign values to HashMap

valueMap.put("PhoneNumber","xxx");
valueMap.put("Email_Address","@com");
.
.
.
valueMap.put("Groups",<Group>Object);

Populate these classes with value and use Google GSON library to convert the hashmap to JSON using

Gson gson=new Gson();
String json=gson.toJson(valueMap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM