Common operations for Fastjson

Source: Internet
Author: User

Reprint: https://www.cnblogs.com/Ming8006/p/6944954.html

Common operations include the following:

    1. The interchange of objects with (Jsonobject or Jsonarray) and string
    2. How string is converted to (Jsonobject or jsonarray) to maintain order
    3. Use of Eval
    4. How to save a value of NULL (Jsonobject or Jsonarray) when converting to string

Code:

Package Demo.sample1;import Com.alibaba.fastjson.json;import Com.alibaba.fastjson.jsonpath;import    Com.alibaba.fastjson.parser.feature;import Com.alibaba.fastjson.serializer.serializerfeature;public class Sample {        public static void Main (string[] args) {Group group = Generategroup (); String ss= "{\" id\ ": 1.1,\" name\ ": \" admin\ ", \" users\ ": [{\" id\ ": 2.2,\" name\ ": \" guest\ "},{\" id\ ": 3.3,\" name\ ": \"                Root\ "}]}";                Group Groupwithnull=generategroupwithnull ();        Basicobjecttostringcompare ();        Jsonobjecttostringcompare (ss);        Njsonobjecttostringcompare (group);                Jsonstringsequence (Group, SS);        Jsoneval (SS, "$.users");            Dealwithnulltostringstring (Groupwithnull); } public static void Basicobjecttostringcompare () {System.out.println ("----------Primitive type toString () ToJSON        String () Comparison:----------");        System.out.println ("Zifuchuan"); System.out.println (json.tojsonstring ("Zifuchuan"));        System.out.println (TRUE);        System.out.println (Json.tojsonstring (true));        SYSTEM.OUT.PRINTLN (1.1);    SYSTEM.OUT.PRINTLN (json.tojsonstring (1.1)); } public static string Njsonobjecttostringcompare (group group) {String jsonstring = json.tojsonstring (group        );        SYSTEM.OUT.PRINTLN ("----------Non-Jsonobject toString () tojsonstring () Comparison:----------");        System.out.println (Group.tostring ());        System.out.println (json.tojsonstring (group));    return jsonstring;        } public static string Jsonobjecttostringcompare (String ss) {Object o=json.parse (Ss,feature.orderedfield);        System.out.println ("----------jsonobject toString () tojsonstring () Comparison:----------");        System.out.println (O.tostring ());        System.out.println (json.tojsonstring (o));    return SS; The public static void Jsonstringsequence (group group, String jsonstring) {System.out.println ("----------has        ----------"); Object O=jSon.parse (jsonstring);                System.out.println (O.tostring ());;        Object O1=json.parse (Jsonstring,feature.orderedfield);                System.out.println (O1.tostring ());        Object O2 = Json.tojson (group);    System.out.println (O2.tostring ()); The public static void Jsoneval (String ss,string jsonPath) {System.out.println ("----------Json Eval:-------                ---");        Object O3=jsonpath.eval (Json.parse (Ss,feature.orderedfield), JSONPath);    System.out.println (O3.tostring ());  } public static void Dealwithnulltostringstring (Group groupwithnull) {System.out.println ("----------Jsonobject        With null Emlement:----------");        System.out.println (json.tojsonstring (groupwithnull));    System.out.println (json.tojsonstring (Groupwithnull,serializerfeature.writemapnullvalue));        } private static Group Generategroup () {Group group = new Group ();        Group.setid (1.1);        Group.setname ("admin"); User GUEStuser = new User ();        Guestuser.setid (2.2);        Guestuser.setname ("Guest");        User Rootuser = new user ();        Rootuser.setid (3.3);        Rootuser.setname ("root");        Group.adduser (Guestuser);        Group.adduser (Rootuser);    return group;        } private static Group Generategroupwithnull () {Group group = new Group ();        Group.setid (1.1);        Group.setname ("");        User GuestUser1 = new user ();        Guestuser1.setid (NULL);        Guestuser1.setname ("Guest");        User GuestUser2 = new user ();                Guestuser2.setid (3.3);                User GuestUser3 = new user ();        Group.adduser (GuestUser1);        Group.adduser (GUESTUSER2);        Group.adduser (GUESTUSER3);    return group; }}

Results:

----------Primitive Type toString () tojsonstring () Comparison:----------Zifuchuan"Zifuchuan"truetrue1.11.1----------jsonobject toString () tojsonstring () Comparison:----------{"id": 1.1, "name": "Admin", "users": [{"id": 2.2, "name": "Guest"},{"id": 3.3, "name": "Root"}]}{"id": 1.1, "name": "Admin", "users": [{"id": 2.2, "name": "Guest"},{"id": 3.3, "name": "Root"}]}----------Non-Jsonobject toString () tojsonstring () Comparison:----------[Email protected]{"id": 1.1, "name": "Admin", "users": [{"id": 2.2, "name": "Guest"},{"id": 3.3, "name": "Root"}]}----------is ordered:----------{"Name": "admin", "id": 1.1, "users": [{"Name": "Guest", "id": 2.2},{"name": "Root", "id": 3.3}]}{"id": 1.1, "name": "Admin", "users": [{"id": 2.2, "name": "Guest"},{"id": 3.3, "name": "Root"}]}{"Name": "admin", "id": 1.1, "users": [{"Name": "Guest", "id": 2.2},{"name": "Root", "id": 3.3}]}----------Json Eval:----------[{"id": 2.2, "name": "Guest"},{"id": 3.3, "name": "Root"}]----------Jsonobject withNULLEmlement:----------{"id": 1.1, "name": "", "users": [{"Name": "Guest"},{"id": 3.3},{}]}{"id": 1.1, "name": "", "users": [{"id":NULL, "name": "Guest"},{"id": 3.3, "name":NULL},{"id":NULL, "name":NULL}]}

Reference:

[1] Introducing JSON

http://www.json.org/

[2] Fastjson Tutorial

Https://www.w3cschool.cn/fastjson/%20

Common operations for 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.