Entity
publicclassUser { privateLong id; privateString name; publicLong getId() { returnid; } publicvoidsetId(Long id) { this.id = id; } publicString getName() { returnname; } publicvoidsetName(String name) { this.name = name; } } |
publicclassGroup { privateLong id; privateString name; privateList<User> users = newArrayList<User>(); publicLong getId() { returnid; } publicvoidsetId(Long id) { this.id = id; } publicString getName() { returnname; } publicvoidsetName(String name) { this.name = name; } publicList<User> getUsers() { returnusers; } publicvoidsetUsers(List<User> users) { this.users = users; } } |
Encode
importcom.alibaba.fastjson.JSON; Group group = newGroup(); group.setId(0L); group.setName("admin"); User guestUser = newUser(); guestUser.setId(2L); guestUser.setName("guest"); User rootUser = newUser(); rootUser.setId(3L); rootUser.setName("root"); group.getUsers().add(guestUser); group.getUsers().add(rootUser); String jsonString = JSON.toJSONString(group); System.out.println(jsonString); |
output
{ "name":"admin", "id":0,"users":[ {"name":"guest","id":2}, {"name":"root","id":3} ] } |
Decode
Group group2 = JSON.parseObject(jsonString, Group.class); |
下载地址:http://code.alibabatech.com/wiki/display/FastJSON/Overview
阅读全文
类别:Json 查看评论