Home >Java >javaTutorial >Detailed introduction to fastjson in java to generate and parse json data (serialization and deserialization data)
This article mainly introduces the generation and parsing of json data (serialization and deserialization data) by fastjson in java. It has certain reference value. Interested friends can refer to it.
This article explains 2 points:
1. Fastjson generates and parses json data
(Example: 4 common types :JavaBean,List36ee8f7418bd46bbf580ced6560b65cf,Listf7e83be87db5cd2d9a8a0b8117b38cd4,Lista06c5c2dfccfdea21bf0b155d9343663)
2. Test the usage of fastjson through an android program.
Introduction to fastjson:
Fastjson is a high-performance and complete JSON library written in Java language. fastjson uses an original algorithm to increase the speed of parse to the extreme, surpassing all json libraries, including jackson, which was once known as the fastest. And it also surpasses Google's binary protocol protocol buf. Fastjson fully supports the standard of http://www.php.cn/ and is also one of the reference implementations included on the official website. Various JDK types are supported. Including basic types, JavaBean, Collection, Map, Enum, generics, etc. Supports JDK 5, JDK 6, Android, Alibaba Cloud mobile phones and other environments.
##1. fastjson generates json string (JavaBean,List36ee8f7418bd46bbf580ced6560b65cf,Listf7e83be87db5cd2d9a8a0b8117b38cd4,Lista06c5c2dfccfdea21bf0b155d9343663)
String jsonStrng = JSON.toJSONString(object);
2. fastjson parses json string into four types
1. JavaBeanPerson person = JSON.parseObject(jsonString, Person.class);2. List36ee8f7418bd46bbf580ced6560b65cf
List<Person> listPerson =JSON.parseArray(jsonString, Person.class);3. Listf7e83be87db5cd2d9a8a0b8117b38cd4
List<String> listString = JSON.parseArray(jsonString, String.class);4. Lista06c5c2dfccfdea21bf0b155d9343663>
Copy code The code is as follows:
List<Map<String, Object>> listMap = JSON.parseObject(jsonString, new TypeReference<List<Map<String,Object>>>(){});(Note: It can be seen here that the fastjson reflection mechanism is more accurate than gson. id = 1001 is still id = 1001 through fastjson reflection, and the result through gson reflection is id =1001.0,
import comalibabafastjsonJSONObject; /** * Created by wangzhenfei on 14-4- */ public class FastJsonTest { public static void main(String[] args){ String jsonStr = "{\"JACKIE_ZHANG\":\"张学友\",\"ANDY_LAU\":\"刘德华\",\"LIMING\":\"黎明\",\"Aaron_Kwok\":\"郭富城\"}" ; //做5次测试 for(int i=0,j=5;i<j;i++) { JSONObject jsonObject = JSONObjectparseObject(jsonStr) ; for(javautilMapEntry<String,Object> entry:jsonObjectentrySet()){ Systemoutprint(entrygetKey()+"-"+entrygetValue()+"\t"); } Systemoutprintln();//用来换行 } } }Running result:
LIMING-黎明 Aaron_Kwok-郭富城JACKIE_ZHANG-张学友ANDY_LAU-刘德华 Aaron_Kwok-郭富城 ANDY_LAU-刘德华LIMING-黎明JACKIE_ZHANG-张学友 Aaron_Kwok-郭富城 JACKIE_ZHANG-张学友ANDY_LAU-刘德华LIMING-黎明 LIMING-黎明 ANDY_LAU-刘德华JACKIE_ZHANG-张学友Aaron_Kwok-郭富城 JACKIE_ZHANG-张学友 LIMING-黎明ANDY_LAU-刘德华Aaron_Kwok-郭富城Solution: Define it as JSONArray, the code is as follows:
import comalibabafastjsonJSONArray; /** * Created by wangzhenfei on 14-4- */ public class FastJsonTest { public static void main(String[] args){ String jsonStr = "[{\"JACKIE_ZHANG\":\"张学友\"},{\"ANDY_LAU\":\"刘德华\"},{\"LIMING\":\"黎明\"},{\"Aaron_Kwok\":\"郭富城\"}]" ; //做5次测试 for(int i=0,j=5;i<j;i++) { JSONArray jsonArray = JSONArrayparseArray(jsonStr); for(int k=0;k<jsonArraysize();k++){ Systemoutprint(jsonArrayget(k) + "\t"); } Systemoutprintln();//用来换行 } } }The running result is:
{"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"} {"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"} {"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"} {"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"} {"JACKIE_ZHANG":"张学友"} {"ANDY_LAU":"刘德华"}{"LIMING":"黎明"}{"Aaron_Kwok":"郭富城"}If you want to define it as JSONObject instead of JSONArray, you can choose other JSON parsers. I personally recommend using Google’s gson. The documentation is obviously much better than fastjson (from here you can see that Ali The gap between Baba and Google):
import comgooglegsonJsonElement; import comgooglegsonJsonObject; import comgooglegsonJsonParser; /** * Created by wangzhenfei on 14-4- */ public class FastJsonTest { public static void main(String[] args){ String jsonStr = "{\"JACKIE_ZHANG\":\"张学友\",\"ANDY_LAU\":\"刘德华\",\"LIMING\":\"黎明\",\"Aaron_Kwok\":\"郭富城\"}" ; //做5次测试 for(int i=0,j=5;i<j;i++) { JsonObject jsonObject = (JsonObject) new JsonParser()parse(jsonStr); for(javautilMapEntry<String,JsonElement> entry:jsonObjectentrySet()){ Systemoutprint(entrygetKey()+"-"+entrygetValue()+"\t"); } Systemoutprintln();//用来换行 } } }Running results:
JACKIE_ZHANG-"张学友" ANDY_LAU-"刘德华" LIMING-"黎明" Aaron_Kwok-"郭富城" JACKIE_ZHANG-"张学友" ANDY_LAU-"刘德华" LIMING-"黎明" Aaron_Kwok-"郭富城" JACKIE_ZHANG-"张学友" ANDY_LAU-"刘德华" LIMING-"黎明" Aaron_Kwok-"郭富城" JACKIE_ZHANG-"张学友" ANDY_LAU-"刘德华" LIMING-"黎明" Aaron_Kwok-"郭富城" JACKIE_ZHANG-"张学友" ANDY_LAU-"刘德华" LIMING-"黎明" Aaron_Kwok-"郭富城"
The above is the detailed content of Detailed introduction to fastjson in java to generate and parse json data (serialization and deserialization data). For more information, please follow other related articles on the PHP Chinese website!