Fastjson is a high-performance, fully-functional JSON library written in the Java language. It uses an algorithm of "Suppose ordered fast matching", which improves the performance of JSON parse to the extreme, and is the fastest JSON library in the Java language today. The Fastjson interface is easy to use and has been widely used in many applications such as cache serialization, protocol interaction, web output, and Android clients.
Pom.xml
Depencency:
<!--Https://mvnrepository.com/artifact/com.alibaba/fastjson--
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.34</version>
</dependency>
Http://www.cnblogs.com/Jie-Jack/p/3758046.html
Serialization of
Serialization refers to a string that turns the JavaBean object into a JSON format.
Com.alibaba.fastjson.JSON provides a number of methods (polymorphic) for serialization.
1. Basic serialization
String Objjson = Json.tojsonstring (Object object);
Pass in an object and turn the object into a JSON string.
Example 1: Turning a map into JSON
1 map<string, object> Map = new hashmap<string, object> (), 2 map.put ("Key1", "One"), 3 Map.put ("Key2", "one"); 4 5 String Mapjson = json.tojsonstring (map);
Output Result:
{"Key1": "One", "Key2": "One"}
Example 2: Turn list<map> into JSON.
1 list<map<string, object>> List = new arraylist<map<string, object>> (); 2 3 map<string, object> map1 = new hashmap<string, object> (), 4 map1.put ("Key1", "one"), 5 map1.put ("Key2" , "both"); 6 7 map<string, object> map2 = new hashmap<string, object> (), 8 map2.put ("Key1", "three"), 9 map2.put ("Key 2 "four"), List.add (MAP1), List.add (MAP2), String Listjson = json.tojsonstring (list);
Output Result:
[{"Key1": "One", "Key2": "Both"},{"Key3": "Three", "Key4": "Four"}]
Example 3: Custom JavaBean user turns JSON.
1 User user = new user (), 2 user.setusername ("John Doe"), 3 User.setage, 4 5 String Userjson = json.tojsonstring (User);
Output Result:
{"Age": $, "userName": "John Doe"}
You can output a formatted JSON string.
String Objjson = Json.tojsonstring (Object object, Boolean Prettyformat);
Passes in an object and a Boolean type (whether formatted), and turns the object into a formatted JSON string.
Example 4: Take example 2 code for example.
String Listjson = json.tojsonstring (list, true);
The output is:
1 [2 {3 "Key1": "One", 4 "Key2": "A", "5 }, 6 {7 " Key3 ":" Three ", 8 " Key4 ":" Four "9 }10 ]
Fastjson provides a number of feature support.
A mutable variable that passes in an object and Serializerfeature type. Serializerfeature is an enumeration.
Com.alibaba.fastjson.serializer.SerializerFeature
You can use these features according to your own situation.
Briefly say a few common features:
1. Date formatting:
Fastjson can be formatted directly on a date type and, by default, Fastjson will convert date to long.
Example 5:fastjson turns java.util.Date into a long.
1 String Datejson = json.tojsonstring (New Date ()); 2 3 System.out.println (Datejson);
Output Result:
1401370199040
Example 6: Formatting dates with the Serializerfeature attribute.
1 String Datejson = json.tojsonstring (New Date (), Serializerfeature.writedateusedateformat); 2 3 System.out.println (Datejson);
Output Result:
"2014-05-29 21:36:24"
You can also specify the output date format.
Example 7: Specify the output date format.
1 String Datejson = Json.tojsonstringwithdateformat (New Date (), "Yyyy-mm-dd HH:mm:ss. SSS "); 2 3 System.out.println (Datejson);
Output Result:
"2014-05-29 21:47:00.154"
2. Use single quotation marks.
Example 8: Take example 2 for example.
String Listjson = json.tojsonstring (list, serializerfeature.usesinglequotes);
Output Result:
[{' Key1 ': ' One ', ' key2 ': ' Both '},{' Key3 ': ' Three ', ' Key4 ': ' Four '}]
3.JSON format.
Example 9:
String Listjson = json.tojsonstring (list, serializerfeature.prettyformat);
Output: Consistent with example 4 results.
4. Output a null field.
By default Fastjson does not enter a field with a value of NULL, you can use Serializerfeature.writemapnullvalue to make it output.
Example 10:
1 map<string, object> Map = new Hashmap<string,object> (); 2 3 String b = null;4 Integer i = 1;5 6 MAP.P UT ("a", b); 7 map.put ("B", I); 8 9 String Listjson = json.tojsonstring (map, Serializerfeature.writemapnullvalue);
Output Result:
{"A": null, "B": 1}
5. Serialization is the write type information.
Example 11:
1 User user = new user (), 2 3 User.setage (), 4 user.setusername ("John Doe"), 5 6 String Listjson = json.tojsonstring (use R, Serializerfeature.writeclassname);
Output Result:
{"@type": "User", "Age": "UserName": "John Doe"}
Because serialization has type information, it enables type recognition to be automated when deserializing.
Example 12: Deserialization of Example 11.
1 User user1 = (user) json.parse (Listjson); 2 3 System.out.println (User1.getage ());
Output Result:
18
If user serialization is not joined to type information (serializerfeature.writeclassname), an error (Java.lang.ClassCastException) will be followed by example 12.
Deserialization
Deserialization is the conversion of a JSON-formatted string into a Java bean object.
Com.alibaba.fastjson.JSON provides a number of methods (polymorphic) for deserialization.
Give a few examples.
Specifies the class information to deserialize.
Example 13: Deserialization of Example 3.
1 User user1 = Json.parseobject (Userjson, User.class); 2 System.out.println (User1.getusername ());
Output Result:
John doe
Sets the deserialization of the collection.
Example 14: Deserialization of Example 2.
1 list<map> list1 = Json.parsearray (Listjson, Map.class); 2 3 for (map<string, object> map:list1) {4 System.out.println (Map.get ("Key1")), 5 System.out.println (Map.get ("Key2")); 4?
Output Result:
1 One2 Two3 Three4 Four
The deserialization of generics (using TypeReference to pass in type information).
Example 15: Deserialization of Example 1.
1 map<string, object> map1 = Json.parseobject (Mapjson, New typereference<map<string, Object>> () {}); 2 System.out.println (Map1.get ("Key1")), 3 System.out.println (Map1.get ("Key2"));
Output Result:
1 One2
--------------------------------------------------------------------------------------------------------------- ------------------------------------------
Jsonobject,jsonarray is the two subclass of JSON.
Jsonobject equivalent to map<string, Object>
Jsonarray equivalent to list<object>.
An example of a simple method:
Example 16: Convert Map to Jsonobject, then add element, output.
1 map<string, object> Map = new hashmap<string, object> (); 2 Map.put ("Key1", "one"); 3 Map.put ("Key2", "both"); 4 5 Jsonobject j = new Jsonobject (map), 6 7 j.put ("Key3", "three"), 8 9 System.out.println (J.get ("Key1")); 10 System.out.println (J.get ("Key2")); System.out.println (J.get ("Key3"));
Output Result:
1 One2 Two3 Three
Example 17: Turn the list object into Jsonarray and output.
1 list<map<string, object>> List = new arraylist<map<string, object>> (); 2 3 map<string, object> Map = new hashmap<string, object> (), 4 map.put ("Key1", "one"), 5 map.put ("Key2", " "); 6 7 map<string, object> map2 = new hashmap<string, object> (), 8 map2.put ("Key1", "three"), 9 map2.put ("Key 2 "four"), list.add (map), List.add (MAP2), jsonarray j = Jsonarray.parsearray (Json.tojsonstring ( list); (int i=0; i<j.size (); i++) { System.out.println (J.get (i)); 18}
Output Result:
1 {"Key1": "One", "Key2": "both"}2 {"Key1": "Three", "Key2": "Four"}
More methods to use please refer to API (no annotated API, which makes me very headache AH).
End
What is Fastjson?