后台json将Date转换成指定格式
我经常用到的后台json转换工具有:fastjson(1.2.47),netsf(2.1),jackson(2.4.1)
一、需求说明
比如:我现在有一个list,里面实体类中有一个属性是Date
我想要转换的最终结果为:
[{"birthday":"1994-03-01 06:20:00","id":101,"name":"张三"},{"birthday":"1995-06-01 06:20:00","id":102,"name":"李四"},{"birthday":"1996-09-01 06:20:00","id":103,"name":"王五"}]
二、代码实现
由于问题比较简单,这三种json工具的代码我直接贴下:
package com.kittycoder.test1;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kittycoder.test1.po.Student_Fj;
import com.kittycoder.test1.po.Student_Jks;
import com.kittycoder.test1.po.Student_Nsf;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* Created by shucheng on 2019-7-22 下午 23:16
*/
public class OutParamTest {
private List list;
// https://blog.csdn.net/qq_41570658/article/details/89383110
@Test
public void fjTest() throws Exception {
list = generateList(Student_Fj.class);
String jsonStr = JSONObject.toJSONString(list);
System.out.println(jsonStr);
}
// https://blessht.iteye.com/blog/2018901
@Test
public void nsfTest() throws Exception {
list = generateList(Student_Nsf.class);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
System.out.println(jsonArray.toString());
}
// https://www.cnblogs.com/wangshen31/p/8961691.html
@Test
public void jksTest() throws Exception {
list = generateList(Student_Jks.class);
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(list);
System.out.println(jsonStr);
}
// 生成测试用的List
private <T> List<T> generateList(final Class<T> clazz) throws Exception {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Constructor<T> ctr = clazz.getDeclaredConstructor(Integer.class, String.class, Date.class);
return new ArrayList<T>(){
{
add(ctr.newInstance(101, "张三", sdf.parse("1994-03-01 06:20:00")));
add(ctr.newInstance(102, "李四", sdf.parse("1995-06-01 06:20:00")));
add(ctr.newInstance(103, "王五", sdf.parse("1996-09-01 06:20:00")));
}};
}
}
class JsonDateValueProcessor implements JsonValueProcessor {
private String format = "yyyy-MM-dd HH:mm:ss";
public JsonDateValueProcessor() {
}
public JsonDateValueProcessor(String format) {
this.format = format;
}
@Override
public Object processArrayValue(Object o, JsonConfig jsonConfig) {
return process(o);
}
@Override
public Object processObjectValue(String s, Object o, JsonConfig jsonConfig) {
return process(o);
}
private Object process(Object value) {
if (value instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
return sdf.format(value);
}
return value == null ? "" : value.toString();
}
}
Student_Fj.java(针对fastjson情形的实体类):
package com.kittycoder.test1.po;
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;
/**
* Created by shucheng on 2019-7-22 下午 22:43
*/
public class Student_Fj {
private Integer id;
private String name;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date birthday; // 假设存的时间是精确到秒的
public Student_Fj() {
}
public Student_Fj(Integer id, String name, Date birthday) {
this.id = id;
this.name = name;
this.birthday = birthday;
}
// 省略setter、getter方法
}
Student_Nsf.java(针对netsf情形的实体类):
package com.kittycoder.test1.po;
import java.util.Date;
/**
* Created by shucheng on 2019-7-22 下午 22:43
*/
public class Student_Nsf {
private Integer id;
private String name;
private Date birthday; // 假设存的时间是精确到秒的
public Student_Nsf() {
}
public Student_Nsf(Integer id, String name, Date birthday) {
this.id = id;
this.name = name;
this.birthday = birthday;
}
// 省略setter、getter方法
}
Student_Jks.java(针对jackson情形的实体类):
package com.kittycoder.test1.po;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
/**
* Created by shucheng on 2019-7-22 下午 22:43
*/
public class Student_Jks {
private Integer id;
private String name;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date birthday; // 假设存的时间是精确到秒的
public Student_Jks() {
}
public Student_Jks(Integer id, String name, Date birthday) {
this.id = id;
this.name = name;
this.birthday = birthday;
}
// 省略setter、getter方法
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kittycoder</groupId>
<artifactId>JSONLibDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- net.sf -->
<net.sf.version>2.1</net.sf.version>
<!-- fastjson -->
<fastjson.version>1.2.47</fastjson.version>
<!-- jackson -->
<jackson.version>2.4.1</jackson.version>
<!-- junit -->
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<!-- net.sf json -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>${net.sf.version}</version>
<classifier>jdk15</classifier>
</dependency>
<!-- Alibaba Fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- jackson相关包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
参考链接:
https://blog.csdn.net/qq_41570658/article/details/89383110
https://blessht.iteye.com/blog/2018901
https://www.cnblogs.com/wangshen31/p/8961691.html
还没有评论,来说两句吧...