1 这段代码主要是利用fastjson的封装方法处理json字符串成java对象之前的相互转换

package com.neo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;
import java.util.Map;

/**
* Created by hqj on 2021/2/5.
*/
public class Json {
private static SerializeConfig config;
private static SerializerFeature[] features = {
//输出空值字段
SerializerFeature.WriteMapNullValue,
//如果数组结果为null,则输出为[],而不是null
SerializerFeature.WriteNullListAsEmpty,
//数值字段为null,则输出为0,而不是null
SerializerFeature.WriteNullNumberAsZero,
//Boolean字段为null,则输出为false,而不是null
SerializerFeature.WriteNullBooleanAsFalse,
//字符类型如果为null,则输出为" ",而不是null
SerializerFeature.WriteNullStringAsEmpty
};
static {
config = new SerializeConfig();
//使用json-lib兼容的日期输出格式
config.put(java.util.Date.class,new JSONLibDataFormatSerializer());
}

/**
* 将一个对象装换为Json字符串
*/
public static String ToJSONString(Object object) {
return JSONObject.toJSONString(object,config,features);
}

/**
* 将Json字符串转换为Object类型的
* */
public static Object ToObject(String str){
return JSON.parse(str);
}

/**
* 将Json字符串转换为实例
* */
public static <T> T ToObject(String str,Class<T> t){
return JSON.parseObject(str,t);
}

/**
* 将Json转换为指定类型的List
* */
public static <T> List<T> ToList(String str, Class<T> t){
return JSON.parseArray(str,t);
}

/**
* 将Json转换为Map
* */
public static <T> Map<String,T> ToMap(String str){
return (Map<String, T>) JSONObject.parseObject(str);
}
}

下面是测试代码

package com.neo.util; 

import com.neo.entity.Media;
import com.neo.entity.News;
import com.neo.entity.SysLog;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* Json Tester.
*
* @author <Authors name>
* @since <pre>02/05/2021</pre>
* @version 1.0
*/
public class JsonTest {

@Before
public void before() throws Exception {
}

@After
public void after() throws Exception {
}

/**
*
* Method: ToJSONString(Object object)
*
*/
@Test
public void testToJSONString() throws Exception {
//TODO: Test goes here...
SysLog log = new SysLog();
log.setUserName("jack");
log.setAction("add");
log.setEvent("insert");
log.setHost("127.0.0.1");
log.setInsertTime(LocalDateTime.now());

String strJson = Json.ToJSONString(log);
System.out.println(strJson);
}

/**
*
* Method: ToObject(String str)
*
*/
@Test
public void testToObjectStr() throws Exception {
String strJson ="{\"action\":\"add\",\"event\":\"insert\",\"host\":\"127.0.0.1\",\"insertTime\":\"2021-02-05T14:58:13.891\",\"logId\":0,\"userName\":\"jack\"}";
Object obj = Json.ToObject(strJson);
System.out.println(obj);
}

/**
*
* Method: ToObject(String str, Class<T> t)
*
*/
@Test
public void testToObjectForStrT() throws Exception {
String strJson ="{\"action\":\"add\",\"event\":\"insert\",\"host\":\"127.0.0.1\",\"insertTime\":\"2021-02-05T14:58:13.891\",\"logId\":0,\"userName\":\"jack\"}";
SysLog obj = Json.ToObject(strJson,SysLog.class);
System.out.println(obj);
}

/**
*
* Method: ToList(String str, Class<T> t)
*
*/
@Test
public void testToList() throws Exception {
/*ArrayList<SysLog> List = new ArrayList<>();
for (int i=0;i<3;i++)
{
SysLog log = new SysLog();
log.setUserName("jack"+i);
log.setAction("add");
log.setEvent("insert");
log.setHost("127.0.0.1");
log.setInsertTime(LocalDateTime.now());
List.add(log);
}
String strJson = Json.ToJSONString(List);

List<SysLog> List2 = Json.ToList(strJson,SysLog.class);

System.out.println(List2);*/

ArrayList<Media> listMedia = new ArrayList<>();
for(int i=0;i<3;i++){
Media media = new Media();
media.setId(i);
media.setTypeId(i);
media.setMemo("test"+i);
media.setPicUrl("/pic/test"+i+".jpg");
listMedia.add(media);
}
News news = new News();
news.setAuthor("jack");
news.setContents("this is a test for news");
news.setTitle("news title");
news.setListMedia(listMedia);

String strJson = Json.ToJSONString(news);

News news2 = Json.ToObject(strJson,News.class);
System.out.println(news2);
}


/**
*
* Method: ToMap(String str)
*
*/
@Test
public void testToMap() throws Exception {
//TODO: Test goes here...
}


}

--- end ---