Small code brother fastjson--Alibaba company Open source the fastest JSON and object conversion tool

Source: Internet
Author: User

Reprint Address: http://www.oschina.net/code/snippet_228315_35122?_t_t_t=0.4667952097048127

	
Class user{private int id;
    private String name;
    public int getId () {return id;
    The public void setId (int id) {this.id = ID;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
}} import Java.util.ArrayList;
 
Import java.util.List;
    Class group{private int id;
    private String name;
    Private list<user> List = new arraylist<user> ();
    public int getId () {return id;
    The public void setId (int id) {this.id = ID;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    Public list<user> GetList () {return List;
    public void setlist (list<user> list) {this.list = list;
    } class Weibo {private String ID;
 
    Private String City;
  Public Weibo (string ID, String city) {this.id = ID;      this.city = City;
    Public Weibo () {} public String GetId () {return id;
    public void SetId (String id) {this.id = ID;
    Public String getcity () {return city;
    public void Setcity (String city) {this.city = city;
}} import Java.util.HashMap;
Import java.util.List;
 
Import Java.util.Map;
Import Com.alibaba.fastjson.JSON;
Import Com.alibaba.fastjson.JSONArray;
 
Import Com.alibaba.fastjson.JSONObject;
The following is a brief introduction to Fastjson: a common method.
The Fastjson API entry class is Com.alibaba.fastjson.JSON, and common serialization operations can be done directly on the static method on the JSON class. public static final Object parse (String text); Parse JSON text as Jsonobject or Jsonarray//public static final Jsonobject parseobject (String text);//parse JSON text to Jsonob Ject//public static final <T> T parseobject (String text, class<t> clazz); Parse the JSON text as JavaBean//public static final Jsonarray Parsearray (String text); Parse JSON text into Jsonarray//public static FInal <T> list<t> Parsearray (String text, class<t> clazz); Parse the JSON text into a JavaBean set//public static final String tojsonstring (object); Serializes JavaBean to JSON text//public static final String tojsonstring (Object object, Boolean Prettyformat); Serializes the JavaBean to the formatted JSON text//public static final object Tojson (object javaobject);
 Convert JavaBean to Jsonobject or Jsonarray (the difference from the above method is that the return value is not the same)/** * This is a demo for Fastjson, validated in the Java environment; * Use this example to recall.
 * Note: The data in JSON format described below is "before", meaning to tell the compiler to ignore it.
         
        * The following definition has three classes: User, Group, Weibo * @author Liuzhao */public class Jsondemo {public static void main (string[] args) { JSON2JSONOBJECTM1 ()//converts JSON text data information to a Jsonobject object and then obtains information json2beanm2 () with a key-value pair;//convert JSON text data to Java
        Bean data. JSON2JSONARRAYM3 ()//converts JSON data to JSONARRAY:JSON2JAVABEANM4 (),//Convert JSON text to JavaBean collection, and convert to Simple 1 object:parseobject ( 
        String text, class<t> clazz); BEAN2JSONM5 ()///convert JavaBean to JSON-formatted data/json text user data delivery: such as uploadingServer BEAN2JSONOBJECTM6 ()//Convert JavaBean to JSON object ComplexExampleM7 ();//A relatively complex example: COMPLEX2JSONM8 ();
    Complex object to JSON demo COMPLEXMAP2JSONM9 ()//convert map data to jsonstring demo; This is not a direct break for the Redboy server.
        /** * Converts JSON text data information into Jsonobject objects, and then obtains information using key-value pairs/private static void Json2jsonobjectm1 () {
        A jsonobject text data String s = "{\" name\ ": \" liuzhao\ "}";
        Converts jsonobject data to JSON Jsonobject object = Json.parseobject (s);
        The Value System.out.println (Object.get ("name") is obtained by means of a key-value pair;
     /** * Print: * Liuzhao/}/** * Converts JSON text data to JavaBean data. * Note that the name of the key in the JSON text message must be the same as the field name in the JavaBean.
     Nothing in the key is displayed as NULL in this JavaBean.
        * * private static void json2beanm2 () {String s = "{\ id\": \ "0375\", \ "city\": \ "Pingdingshan \"};
        A simple and convenient way to convert JSON text information to a Jsonobject object while converting it to a JavaBean object. Weibo Weibo = Json.parseobject (s, weibo.class);//weibo class is defined below SYSTEM.OUt.println (Weibo.getid ());
        System.out.println (Weibo.getcity ()); Results printed 0375//Pingdingshan/** * Convert map type data to jsonstring/private static VO
        ID complexmap2jsonm9 () {Group group = new Group ();
        Group.setid (1);
         
        Group.setname ("group");
        User User1 = new user ();
        User1.setid (2);
 
        User1.setname ("user1");
        User User2 = new user ();
        User2.setid (3);
        User2.setname ("User2");
        Group.getlist (). Add (User1);
         
        Group.getlist (). Add (User2);
        Map<integer, object> map = new hashmap<integer,object> ();
        Map.put (1, "No.1");
        Map.put (2, "No.2");
         
        Map.put (3, Group.getlist ());
        String jsonstring = json.tojsonstring (map);
        System.out.println (jsonstring);
     /** * Output: {1: "No.1", 2: "No.2", 3:[{"id": 2, "name": "user1"},{"id": 3, "name": "User2"}]} */}/** * by aComplex object to JSON demo */private static void Complex2jsonm8 () {Group group = new Group ();
        Group.setid (1);
         
        Group.setname ("group");
        User User1 = new user ();
        User1.setid (2);
 
        User1.setname ("user1");
        User User2 = new user ();
        User2.setid (3);
        User2.setname ("User2");
        Group.getlist (). Add (User1);
        Group.getlist (). Add (User2);
        String jsonstring = json.tojsonstring (group);
        System.out.println (jsonstring);
 
    /** * Output: {"id": 1, "list": [{"id": 2, "name": "user1"},{"id": 3, "name": "User2"}], "name": "Group"}/*
     /** * A relatively complex example: * JSON text data: * First is jsonobject, convert to Jsonarray; * then convert Jsonarray to JavaBean * * private static void ComplexExampleM7 () {String s = "{js:[{id:\" 110000\ "," city\ ": \" North #001 jing Shi "},{id:\" 110000 \ ", \" city\ ": \" North #002 jing Shi "}" + ", {id:\" 110000\ "," city\ ": \" North #002 jing Shi "},{id:\" 110000\ "," city\ ": \"North #002 jing Shi"}, "+" {id:\ "110000\", \ "city\": \ "#006北 #005 jing Shi"}, "+" {id:\ "110000\", \ "city\": \ "North
         
        #002京市 \ "}," + "{id:\" 110000\ ", \" city\ ": \" North #002 jing Shi "},{id:\" 120000\ ", \" city\ ": \" Days #009 Jinshi \ "}]}";
        Jsonobject object = Json.parseobject (s);
        Object Jsonarray = Object.get ("JS");
        System.out.println (Jsonarray);
        list<weibo> list = Json.parsearray (jsonarray+ "", Weibo.class);
        for (Weibo weibo:list) {System.out.println (weibo.getcity ()); }/** * Converts javabean to JSON object/private static void Bean2jsonobjectm6 () {Weibo Weibo = NE
        W Weibo ("0373", "Luoyang");
        Jsonobject json = (jsonobject) Json.tojson (Weibo);
        System.out.println (Json.get ("id")); /** * Print: * 0373/}/** * Converts JSON text to a set of javabean; * Internal implementation must be: first convert to Jsonarray , then convert to list collection/private static void Json2javabeanm4 () {StrinG S = "[{] id\": \ "0375\", "city\": "Pingdingshan \"},{\ "id\": \ "0377\", "city\": "Nanyang \"}];
        list<weibo> list = Json.parsearray (s, weibo.class);
        for (Weibo weibo:list) {System.out.println (weibo.getcity ()); /** * Print: * Pingdingshan * Nanyang/}/** * Convert JSON data to Jsonarray: * Note
     After getting to Jsonarray (we just get the list of Jsonarray instead of JavaBean) * Get the data in the Jsonarray to be converted to a string type need to be added outside ""; * * private static void Json2jsonarraym3 () {String s = "[{\ id\": \ "0375\", \ "city\": \ "Pingdingshan \"},{\ "id\": \ "0377\", \
        "city\": \ "Nanyang \"}];
        Converts JSON text to Jsonarray jsonarray array = Json.parsearray (s);
        This line must be written: + "" must be added, or the type of strong-turn exception will be reported.
        String str = array.get (1) + "";
        System.out.println (array.get (0));
        Jsonobject object = Json.parseobject (str);
        System.out.println (Object.get ("id")); /** * {"City": "Pingdingshan", "id": "0375"} 0377 * */** * will JAvabean convert to JSON-formatted data/json text * User data transfer: For example, upload server/private static void Bean2jsonm5 () {Weibo Weibo = NE
        W Weibo ("123456", "Shanghai");
        String string = Json.tojsonstring (Weibo);
        System.out.println (string);
 /** * Print: * {"City": "Shanghai", "id": "123456"} * *}}

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.