Fastjson Using Tutorials

Source: Internet
Author: User

Fastjson is Ali's Open source project, with the online version of the Fastjson resolution is Gson 6 times times, small size, and open source.

Project Address:

Https://github.com/alibaba/fastjson

The general JSON parser we all use is serialization and deserialization.

    • Json.tojsonstring
    • Parseobject.

Let's take a look at the specific usage of Fastjson in the following example:

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;    }}

  

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);

The above code input results are as follows:

{"id": 0, "name": "Admin", "users": [{"id": 2, "name": "Guest"},{"id": 3, "name": "Root"}]}

The deserialization operation is as follows:

Group Group = Json.parseobject (jsonstring, Group.class);

  

Fastjson Using Tutorials

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.