Home > Java > javaTutorial > Detailed introduction to fastjson in java to generate and parse json data (serialization and deserialization data)

Detailed introduction to fastjson in java to generate and parse json data (serialization and deserialization data)

黄舟
Release: 2017-03-09 10:04:50
Original
1981 people have browsed it

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,List,List,List)

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,List,List,List)

String jsonStrng = JSON.toJSONString(object);
Copy after login

2. fastjson parses json string into four types

1. JavaBean

Person person = JSON.parseObject(jsonString, Person.class);
Copy after login

2. List

List<Person> listPerson =JSON.parseArray(jsonString, Person.class);
Copy after login

3. List

List<String> listString = JSON.parseArray(jsonString, String.class);
Copy after login

4. List>

Copy code The code is as follows:

List<Map<String, Object>> listMap = JSON.parseObject(jsonString, new TypeReference<List<Map<String,Object>>>(){});
Copy after login

(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,


JSON parser fastjson (produced by Alibaba, version 1.1.26), if the JSONObject is defined as {"JACKIE_ZHANG":"Jacky Cheung","ANDY_LAU":"Andy Lau","LIMING":"Ling Ming","Aaron_Kwok":" Aaron Kwok"}, then when reading the value, the KEY order is out of order, test code:

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();//用来换行 
  } 
 } 
}
Copy after login

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-郭富城
Copy after login

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();//用来换行 
  } 
 } 
}
Copy after login

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":"郭富城"}
Copy after login

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();//用来换行 
  } 
 } 
}
Copy after login

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-"郭富城"
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template