Fastjson Sample Code

Source: Internet
Author: User

package com.wzc.test.test;import java.text.simpledateformat;import java.util.date;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;import  com.alibaba.fastjson.typereference;import com.alibaba.fastjson.serializer.serializerfeature;class  user {private int id;private string name;private date birthday;public  Int getid ()  {return id;} Public void setid (Int id)  {this.id = id;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public date getbirthday ()  {return birthday;} Public void setbirthday (Date birthday)  {this.birthday = birthday;} @Overridepublic  string tostring ()  {simplEdateformat format = new simpledateformat ("Yyyy-mm-dd");return  "User [id="   + id +  ",  name="  + name +  ",  birthday="  + format.format (birthday)  +  "]";}} Public class app {public static void main (String[] args)  {//  Serialization and deserialization bean//  because the fields in the bean have types, different types can handle the// 1.  prepare data correctly user user = new user (); User.setid (1); User.setname ("user"); User.setbirthday (New date ());// 2.  serialization String beanstr  = json.tojsonstring (user); System.out.println (BEANSTR); System.out.println ();// 3.  deserialization user bean = json.parseobject (Beanstr, user.class) ; System.out.println (Bean); System.out.println ();// 4.  a formatted form of deserialization string prettystr = json.tojsonstring (user,  Serializerfeature.prettyformat); System.out.println (PRETTYSTR); SYSTEM.OUT.PRINTLN ();// 5.  use parameters to preserve type information String&nbsP;classstr = json.tojsonstring (user, serializerfeature.writeclassname,  Serializerfeature.prettyformat); System.out.println (CLASSSTR); System.out.println ();//  serialization and deserialization map// 1.  preparing data map<string, object> map =  new HashMap<> (); Map.put ("id",  1), Map.put ("name",  "User"); Map.put ("Birthday",  New date ());// 2.  serialization string mapstr = json.tojsonstring (map); System.out.println (MAPSTR); SYSTEM.OUT.PRINTLN ();// 3.  deserializes//  uses typereference to provide type information//  because value is of type object, You can see that serialization and deserialization are missing type information Map<string, object> m = json.parseobject (mapstr, new  Typereference<map<string, object>> ()  {}); System.out.println (m); SYSTEM.OUT.PRINTLN ();//  serialize and deserialize the array// 1.  prepare the data user user2 = new user (); User2.setid (2); User2.setname ("User2"); User2.setbirthday (New date ()); user[] array = new user[] { user, user2 };// 2.  serialization string arraystr = json.tojsonstring (array); System.out.println (ARRAYSTR); System.out.println ();// 3.  deserialization List<user> users = json.parsearray (ARRAYSTR,  user.class); System.out.println (users); SYSTEM.OUT.PRINTLN ();// 4.  use Jsonobject and jsonarrayjsonarray jsonarray =  Json.parsearray (ARRAYSTR);for  (Int i = 0; i < jsonarray.size ();  i++ )  {jsonobject obj = jsonarray.getjsonobject (i); System.out.println (obj);}}}

Output

{"Birthday": 1453037936843, "id": 1, "name": "User"} User [Id=1, Name=user, birthday=2016-01-17]{"Birthday": 1453037936843, "id": 1, "name": "User"} {"@type": " Com.wzc.test.Test.User "," Birthday ": 1453037936843," id ": 1," name ":" User "} {" Birthday ": 1453037937054," name ":" User ", "id": 1}{birthday=1453037937054, Name=user, id=1}[{"Birthday": 1453037936843, "id": 1, "name": "User"},{"Birthday" : 1453037937057, "id": 2, "name": "User2"}][user [Id=1, Name=user, BIRTHDAY=2016-01-17], User [id=2, Name=user2, birthday= 2016-01-17]]{"Birthday": 1453037936843, "name": "User", "id": 1}{"Birthday": 1453037937057, "name": "User2", "id": 2}


This article is from the "self-reliance, tenet" blog, please be sure to keep this source http://wangzhichao.blog.51cto.com/2643325/1735963

Fastjson Sample Code

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.