JSON parsing scheme from Ali Fastjson

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.