fastjson带子集的json

前端之家收集整理的这篇文章主要介绍了fastjson带子集的json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public class User {
    private Long id;
    private String name;
 
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
 
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

public class Group {
    private Long id;
    private String name;
    private List<User> users = new ArrayList<User>();
 
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
 
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
 
    public List<User> getUsers() { return users; }
    public void setUsers(List<User> users) { this.users = users; }
}

import com.alibaba.fastjson.JSON;
 
Group group = new Group();
group.setId(0L);
group.setName("admin");
 
User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");
 
User rootUser = new User();
rootUser.setId(3L);
rootUser.setName("root");
 
group.getUsers().add(guestUser);
group.getUsers().add(rootUser);
 
String jsonString = JSON.toJSONString(group);
 
System.out.println(jsonString);

{
      "name":"admin","id":0,"users":[
                             {"name":"guest","id":2},{"name":"root","id":3}
                         ]
}

Group group2 = JSON.parSEObject(jsonString,Group.class);
原文链接:https://www.f2er.com/json/289205.html

猜你在找的Json相关文章