fastjson in java generates and parses json data (serialized and deserialized data)

  • 2020-06-07 04:25:34
  • OfStack

This article explains two points:

1. fastjson generates and parses json data

(Example: Four common types :JavaBean,List < JavaBean > ,List < String > ,List < Map < String,Object > )

2. Test the usage of fastjson through 1 android program.

fastjson introduction:

Fastjson is a high performance and complete JSON library written in Java. fastjson USES its original algorithm to push parse to the top of all json libraries, including jackson, which was once claimed to be the fastest. It also goes beyond the base 2 protocol of google, protocol buf. Fastjson fully supports the standard http:// ES44en.org, which is also included in the official website as a reference implementation 1. Support for various JDK types. Including basic types, JavaBean, Collection, Map, Enum, generic, etc. Support JDK 5, JDK 6, Android, Aliyun mobile phone environment.

1. fastjson generates json string (JavaBean,List) < JavaBean > ,List < String > ,List < Map < String,Object > )


String jsonStrng = JSON.toJSONString(object);

fastjson parses the json string into four types

1. JavaBean


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

2. List < JavaBean >


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

3. List < String >


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

4. List < Map < String,Object > >


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 the reflection from fastjson is still id =1001, while the reflection from gson =1001.0,

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


import comalibabafastjsonJSONObject; 
/** 
 * Created by wangzhenfei on 14-4- 
 */ 
public class FastJsonTest { 
 public static void main(String[] args){ 
  String jsonStr = "{\"JACKIE_ZHANG\":\" Jacky cheung \",\"ANDY_LAU\":\" Andy lau \",\"LIMING\":\" The dawn \",\"Aaron_Kwok\":\" Aaron kwok \"}" ; 
 
 
  // do 5 The test  
  for(int i=0,j=5;i<j;i++) 
  { 
   JSONObject jsonObject = JSONObjectparseObject(jsonStr) ; 
   for(javautilMapEntry<String,Object> entry:jsonObjectentrySet()){ 
    Systemoutprint(entrygetKey()+"-"+entrygetValue()+"\t"); 
   } 
   Systemoutprintln();// Used to wrap  
  } 
 } 
} 

Operation results:


LIMING- The dawn  Aaron_Kwok- Aaron kwok JACKIE_ZHANG- Jacky cheung ANDY_LAU- Andy lau  
Aaron_Kwok- Aaron kwok  ANDY_LAU- Andy lau LIMING- The dawn JACKIE_ZHANG- Jacky cheung  
Aaron_Kwok- Aaron kwok  JACKIE_ZHANG- Jacky cheung ANDY_LAU- Andy lau LIMING- The dawn  
LIMING- The dawn  ANDY_LAU- Andy lau JACKIE_ZHANG- Jacky cheung Aaron_Kwok- Aaron kwok  
JACKIE_ZHANG- Jacky cheung  LIMING- The dawn ANDY_LAU- Andy lau Aaron_Kwok- Aaron kwok  

Solution: Define as JSONArray with the following code:


import comalibabafastjsonJSONArray; 
 
/** 
 * Created by wangzhenfei on 14-4- 
 */ 
public class FastJsonTest { 
 public static void main(String[] args){ 
  String jsonStr = "[{\"JACKIE_ZHANG\":\" Jacky cheung \"},{\"ANDY_LAU\":\" Andy lau \"},{\"LIMING\":\" The dawn \"},{\"Aaron_Kwok\":\" Aaron kwok \"}]" ; 
  // do 5 The test  
  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();// Used to wrap  
  } 
 } 
} 

The operation result is:


{"JACKIE_ZHANG":" Jacky cheung "} {"ANDY_LAU":" Andy lau "}{"LIMING":" The dawn "}{"Aaron_Kwok":" Aaron kwok "} 
{"JACKIE_ZHANG":" Jacky cheung "} {"ANDY_LAU":" Andy lau "}{"LIMING":" The dawn "}{"Aaron_Kwok":" Aaron kwok "} 
{"JACKIE_ZHANG":" Jacky cheung "} {"ANDY_LAU":" Andy lau "}{"LIMING":" The dawn "}{"Aaron_Kwok":" Aaron kwok "} 
{"JACKIE_ZHANG":" Jacky cheung "} {"ANDY_LAU":" Andy lau "}{"LIMING":" The dawn "}{"Aaron_Kwok":" Aaron kwok "} 
{"JACKIE_ZHANG":" Jacky cheung "} {"ANDY_LAU":" Andy lau "}{"LIMING":" The dawn "}{"Aaron_Kwok":" Aaron kwok "} 

If you want to define JSONObject instead of JSONArray, you can use another JSON parser. Personally, I recommend google's gson. The documentation is much better than fastjson (you can see the gap between alibaba and Google here) :


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\":\" Jacky cheung \",\"ANDY_LAU\":\" Andy lau \",\"LIMING\":\" The dawn \",\"Aaron_Kwok\":\" Aaron kwok \"}" ; 
  // do 5 The test  
  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();// Used to wrap  
  } 
 } 
} 

Operation results:


JACKIE_ZHANG-" Jacky cheung " ANDY_LAU-" Andy lau " LIMING-" The dawn " Aaron_Kwok-" Aaron kwok "  
JACKIE_ZHANG-" Jacky cheung " ANDY_LAU-" Andy lau " LIMING-" The dawn " Aaron_Kwok-" Aaron kwok "  
JACKIE_ZHANG-" Jacky cheung " ANDY_LAU-" Andy lau " LIMING-" The dawn " Aaron_Kwok-" Aaron kwok "  
JACKIE_ZHANG-" Jacky cheung " ANDY_LAU-" Andy lau " LIMING-" The dawn " Aaron_Kwok-" Aaron kwok "  
JACKIE_ZHANG-" Jacky cheung " ANDY_LAU-" Andy lau " LIMING-" The dawn " Aaron_Kwok-" Aaron kwok "

Related articles: