Speaking of JSON parsing, there are many ways, whether it is from Google's Gson, or from other xxx, presumably everyone is familiar. Strolling around GitHub today, encountering a JSON parsing library, looks pretty good, and is said to be the fastest analytic josn solution available. Why the artifact, he is from the Ali team Fastjson.
For the sake of convenience, refer to the following code.
With Maven's classmates is more convenient, directly double-click Open your Pom.xml file, add the following dependencies
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.36</version></dependency>
It's OK to build a bit.
Here's a concrete look:
ENCODE:
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);
OUTPUT:
{"id":0,"name":"admin","users":[{"id":2,"name":"guest"},{"id":3,"name":"root"}]}
DECODE:
String jsonString = ...;Group group = JSON.parseObject(jsonString, Group.class);
Group.java
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; }}
User.java
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; }}
Very useful!
For more details, refer to: Https://github.com/alibaba/fastjson