Fastjson object or array to JSON


Fastjson A json library open sourced by Alibaba engineers: Fastjson. This library is very good in terms of parsing speed and ease of use.

In log parsing and front-end and back-end data transmission interactions, we often encounter scenarios of mutual conversion and parsing between String and map, json, xml and other formats. Among them, json has basically become a cross-language, cross-front-end and cross-end de facto standard data interaction format. It should be said that there are a large number of libraries for parsing json in various languages ​​(for an introduction to the specific json format and third-party libraries, please see: http://www.json.org/json-zh.html). For example, python is integrated into the built-in library. , it has become a standard API. Today we are going to talk about how to conveniently use json format in java.

The following example is how to use Fastjson to convert objects or arrays to JSON

package test;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import com.alibaba.fastjson.JSON;  
  
class User {  
    private String name;  
    private int age;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
  
    @Override  
    public String toString() {  
        return "User [name=" + name + ", age=" + age + "]";  
    }  
};  
  
class UserGroup {  
    private String name;  
    private List<User> users = new ArrayList<User>();  
  
    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;  
    }  
  
    @Override  
    public String toString() {  
        return "UserGroup [name=" + name + ", users=" + users + "]";  
    }  
}  
  
class FastJsonTest {  
    public static void main(String[] args) {  
        // 构建用户geust  
        User guestUser = new User();  
        guestUser.setName("guest");  
        guestUser.setAge(28);  
        // 构建用户root  
        User rootUser = new User();  
        rootUser.setName("root");  
        guestUser.setAge(35);  
        // 构建用户组对象  
        UserGroup group = new UserGroup();  
        group.setName("admin");  
        group.getUsers().add(guestUser);  
        group.getUsers().add(rootUser);  
        // 用户组对象转JSON串  
        String jsonString = JSON.toJSONString(group);  
        System.out.println("jsonString:" + jsonString);  
        // JSON串转用户组对象  
        UserGroup group2 = JSON.parseObject(jsonString, UserGroup.class);  
        System.out.println("group2:" + group2);  
  
        // 构建用户对象数组  
        User[] users = new User[2];  
        users[0] = guestUser;  
        users[1] = rootUser;  
        // 用户对象数组转JSON串  
        String jsonString2 = JSON.toJSONString(users);  
        System.out.println("jsonString2:" + jsonString2);  
        // JSON串转用户对象列表  
        List<User> users2 = JSON.parseArray(jsonString2, User.class);  
        System.out.println("users2:" + users2);  
    }  
}
output results:


jsonString:{"name":"admin","users":[{"age":35,"name":"guest"},{"age":0,"name":"root"}]}  
group2:UserGroup [name=admin, users=[User [name=guest, age=35], User [name=root, age=0]]]  
jsonString2:[{"age":35,"name":"guest"},{"age":0,"name":"root"}]  
users2:[User [name=guest, age=35], User [name=root, age=0]]
fastjson has passed all tests Very good, the function and performance are No. 1, I like it, its source code quality is very high, the author has also taken great pains to achieve the best performance, without surpassing other json libraries.
We can develop quickly through fastjson.