Use of Alibaba's FastJson package

Keywords: JSON Java Attribute

2. Introduction to Alibaba's FastJson package.

The Fastjson API entry class is com.alibaba.fastjson.JSON. Common serialization operations can be directly completed in static methods on JSON classes.
SerializeWriter: equivalent to StringBuffer
JSONArray: equivalent to List
JSONObject: equivalent to map < string, Object >
There is no real array in JSON deserialization, and the intrinsic type is List.

// Make JSON text parse JSONObject or JSONArray 
public static final Object parse(String text);
// parse JSON text into JSONObject 
public static final JSONObject parseObject(String text); 
// Make JSON text parse JavaBean 
public static final <T> T parseObject(String text, Class<T> clazz);
// parse JSON text into JSONArray 
public static final JSONArray parseArray(String text); 
//parse JSON text into JavaBean collection 
public static final <T> List<T> parseArray(String text, Class<T> clazz); 
// Serialize JavaBean s into JSON text 
public static final String toJSONString(Object object); 
// Serialize JavaBean s into formatted JSON text 
public static final String toJSONString(Object object, boolean prettyFormat); 
//Convert JavaBean s to jsonobjects or jsonarrays.
public static final Object toJSON(Object javaObject); 

Required dependencies:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.28</version>
</dependency>

(3)bean conversion json
Converting objects to formatted json

JSON.toJSONString(obj,true);

(4)json conversion bean

String json = "{\"id\":\"2\",\"name\":\"Json technology\"}";
Book book = JSON.parseObject(json, Book.class);

a) get attributes from json string

String propertyName = 'id';
String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json technology\"}";
JSONObject obj = JSON.parseObject(json);
propertyValue = obj.get(propertyName));

Convert objects to unformatted json

JSON.toJSONString(obj,false);

obj design object
For conversion of complex types, for repeated references, reference characters appear in the json string after conversion, such as ref & quote;: & quote; ref & quote;: & quote; ref ":" [0]. Books [1]

Student stu = new Student();
Set books= new HashSet();
Book book = new Book();
books.add(book);
stu.setBooks(books);
List list = new ArrayList();
for(int i=0;i<5;i++)
list.add(stu);
String json = JSON.toJSONString(list,true);

(5)json transforms complex bean s, such as List and Map

String json = "[{\"id\":\"1\",\"name\":\"Json technology\"},{\"id\":\"2\",\"name\":\"java technology\"}]";
//Convert json to List
List list = JSON.parseObject(json,new TypeReference<ARRAYLIST>(){});
//Convert json to Set
Set set = JSON.parseObject(json,new TypeReference<HASHSET>(){});

(6) directly operate json through json object

b) remove an attribute in json

String propertyName = 'id';
String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json technology\"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
propertyValue = set.remove(propertyName);
json = obj.toString();

c) add attributes to json

String propertyName = 'desc';
Object propertyValue = "json Gadgets";
String json = "{\"id\":\"1\",\"name\":\"Json technology\"}";
JSONObject obj = JSON.parseObject(json);
obj.put(propertyName, JSON.toJSONString(propertyValue));
json = obj.toString();

d) modify attributes in json

String propertyName = 'name';
Object propertyValue = "json Gadgets";
String json = "{\"id\":\"1\",\"name\":\"Json technology\"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
if(set.contains(propertyName))
obj.put(propertyName, JSON.toJSONString(propertyValue));
json = obj.toString();

e) judge whether there are attributes in json

String propertyName = 'name';
boolean isContain = false;
String json = "{\"id\":\"1\",\"name\":\"Json technology\"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
isContain = set.contains(propertyName);

F) processing of date format in JSON

Object obj = new Date();
String json = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss.SSS");

Using JSON.toJSONStringWithDateFormat, this method can transform the date using the set date format

Posted by evanesq on Thu, 17 Oct 2019 13:25:08 -0700