The
looks like this:
Package Demo;
Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Vector;
Import Com.alibaba.fastjson.JSON;
Import Com.alibaba.fastjson.JSONArray;
Import Com.alibaba.fastjson.JSONObject;
Import Com.alibaba.fastjson.serializer.SerializerFeature; Import entity.
Userinfo; /** * Fastjson is a very good Java language implementation of the JSON parser and generator, from the Alibaba engineer Development. Main Features: * 1 quick fast (faster than any other java-based parser and generator, including Jackson) strong (support for common JDK classes including any Java Bean * 2.Class, Collection, Map, date, or enum) 0 Dependencies (no other class library except JDK) */public class Testfastjson {public static void main (string[] args) {String json = ' {\
"name\": \ "chenggang\", \ "age\": 24} ";
String Arrayayy = "[[\ Ma Yun ', 50],null,[\ ' ma Teng ', 30]]";
Entity2json ("Zhangsan", 24);
List2json ();
Complexdata ();
Deserialization (JSON);
Dateformate (New Date ());
Json2eetity (JSON);
String2jsonarray (ARRAYAYY); }//entity to JSON public static void Entity2json(String name, int age)
{Userinfo info = new Userinfo (name, age); String Str_json = json.tojsonstring (info);
System.out.println ("Convert entity to JSON" + Str_json);
}//List turn JSON public static void List2json () {list<userinfo> list = new arraylist<userinfo> ();
Userinfo userinfo1 = new Userinfo ("Lisi", 15);
Userinfo Userinfo2 = new Userinfo ("Wangwu", 16);
List.add (USERINFO1);
List.add (Userinfo2);
String json = json.tojsonstring (list, true);
System.out.println ("The list set to the JSON format string:" + JSON); }//character array into JSON private static void String2jsonarray (String arrayayy) {Jsonarray array = Jsonarray.parsearray (a
RRAYAYY);
System.out.println ("array:" + array);
SYSTEM.OUT.PRINTLN ("Array length:" + array.size ());
Collection Nucon = new Vector ();
Nucon.add (NULL);
Array.removeall (Nucon);
System.out.println ("array:" + array);
SYSTEM.OUT.PRINTLN ("Array length:" + array.size ()); }//Complex data type public static void Complexdata () {Hashmap<string, object> map = new hashmap<string, object> ();
Map.put ("username", "Zhangsan");
Map.put ("Age", 24);
Map.put ("Sex", "male");
Map set hashmap<string, object> temp = new hashmap<string, object> ();
Temp.put ("name", "Xiaohong");
Temp.put ("Age", "23");
Map.put ("Girlinfo", temp);
List set list<string> list = new arraylist<string> ();
List.add ("mountain climbing");
List.add ("cycling");
List.add ("tourism");
Map.put ("hobby", list);
String jsonstring = json.tojsonstring (map);
SYSTEM.OUT.PRINTLN ("Complex data type:" + jsonstring);
public static void deserialization (String json) {Userinfo Userinfo = Json.parseobject (JSON, userinfo.class);
System.out.println ("name is:" + userinfo.getname () + ", Age is:" + userinfo.getage ());
}//Format session date public static void Dateformate (date date) {System.out.println ("Output millisecond value:" + json.tojsonstring (date)); SYSTEM.OUT.PRINTLN ("Default format is:" + json.tojsonstring (date, Serializerfeature.writedateusedateformat)); SYSTEM.OUT.PRINTLN ("Custom Date:" + Json.tojsonstringwithdateformat (date, "Yyyy-mm-dd", SERIALIZERFEATURE.WR
Itedateusedateformat)); }//JSON is converted to entity private static void Json2eetity (String json) {Userinfo Userinfo = Json.parseobject (JSON, Userinfo.
Class);
System.out.println ("The address of the output object:" + userinfo.tostring ());
System.out.println ("The name of the output object:" + userinfo.getname ()); }
}
The entity classes used in the demo above are:
Package entity;
public class Userinfo {
private static final long serialversionuid = 1L;
private String name;
private int age;
Public Userinfo () {
super ();
}
Public Userinfo (String name, int age) {
super ();
this.name = name;
This.age = age;
}
public void SetName (String name) {
this.name = name;
}
Public String GetName () {return
name;
}
public void Setage (int age) {
this.age = age;
}
public int getage () {return age
;
}
}
The above article on the common use of Fastjson is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.