后台json将Date转换成指定格式

你的名字 2021-09-30 04:12 587阅读 0赞

我经常用到的后台json转换工具有:fastjson(1.2.47),netsf(2.1),jackson(2.4.1)

一、需求说明

比如:我现在有一个list,里面实体类中有一个属性是Date

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA5OTk4MDk_size_16_color_FFFFFF_t_70

我想要转换的最终结果为:

  1. [{"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":"王五"}]

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA5OTk4MDk_size_16_color_FFFFFF_t_70 1

二、代码实现

由于问题比较简单,这三种json工具的代码我直接贴下:

  1. package com.kittycoder.test1;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.kittycoder.test1.po.Student_Fj;
  5. import com.kittycoder.test1.po.Student_Jks;
  6. import com.kittycoder.test1.po.Student_Nsf;
  7. import net.sf.json.JSONArray;
  8. import net.sf.json.JsonConfig;
  9. import net.sf.json.processors.JsonValueProcessor;
  10. import org.junit.Test;
  11. import java.lang.reflect.Constructor;
  12. import java.text.SimpleDateFormat;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.List;
  16. import java.util.Locale;
  17. /**
  18. * Created by shucheng on 2019-7-22 下午 23:16
  19. */
  20. public class OutParamTest {
  21. private List list;
  22. // https://blog.csdn.net/qq_41570658/article/details/89383110
  23. @Test
  24. public void fjTest() throws Exception {
  25. list = generateList(Student_Fj.class);
  26. String jsonStr = JSONObject.toJSONString(list);
  27. System.out.println(jsonStr);
  28. }
  29. // https://blessht.iteye.com/blog/2018901
  30. @Test
  31. public void nsfTest() throws Exception {
  32. list = generateList(Student_Nsf.class);
  33. JsonConfig jsonConfig = new JsonConfig();
  34. jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
  35. JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
  36. System.out.println(jsonArray.toString());
  37. }
  38. // https://www.cnblogs.com/wangshen31/p/8961691.html
  39. @Test
  40. public void jksTest() throws Exception {
  41. list = generateList(Student_Jks.class);
  42. ObjectMapper mapper = new ObjectMapper();
  43. String jsonStr = mapper.writeValueAsString(list);
  44. System.out.println(jsonStr);
  45. }
  46. // 生成测试用的List
  47. private <T> List<T> generateList(final Class<T> clazz) throws Exception {
  48. final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  49. final Constructor<T> ctr = clazz.getDeclaredConstructor(Integer.class, String.class, Date.class);
  50. return new ArrayList<T>(){
  51. {
  52. add(ctr.newInstance(101, "张三", sdf.parse("1994-03-01 06:20:00")));
  53. add(ctr.newInstance(102, "李四", sdf.parse("1995-06-01 06:20:00")));
  54. add(ctr.newInstance(103, "王五", sdf.parse("1996-09-01 06:20:00")));
  55. }};
  56. }
  57. }
  58. class JsonDateValueProcessor implements JsonValueProcessor {
  59. private String format = "yyyy-MM-dd HH:mm:ss";
  60. public JsonDateValueProcessor() {
  61. }
  62. public JsonDateValueProcessor(String format) {
  63. this.format = format;
  64. }
  65. @Override
  66. public Object processArrayValue(Object o, JsonConfig jsonConfig) {
  67. return process(o);
  68. }
  69. @Override
  70. public Object processObjectValue(String s, Object o, JsonConfig jsonConfig) {
  71. return process(o);
  72. }
  73. private Object process(Object value) {
  74. if (value instanceof Date) {
  75. SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
  76. return sdf.format(value);
  77. }
  78. return value == null ? "" : value.toString();
  79. }
  80. }

Student_Fj.java(针对fastjson情形的实体类):

  1. package com.kittycoder.test1.po;
  2. import com.alibaba.fastjson.annotation.JSONField;
  3. import java.util.Date;
  4. /**
  5. * Created by shucheng on 2019-7-22 下午 22:43
  6. */
  7. public class Student_Fj {
  8. private Integer id;
  9. private String name;
  10. @JSONField(format = "yyyy-MM-dd HH:mm:ss")
  11. private Date birthday; // 假设存的时间是精确到秒的
  12. public Student_Fj() {
  13. }
  14. public Student_Fj(Integer id, String name, Date birthday) {
  15. this.id = id;
  16. this.name = name;
  17. this.birthday = birthday;
  18. }
  19. // 省略setter、getter方法
  20. }

Student_Nsf.java(针对netsf情形的实体类):

  1. package com.kittycoder.test1.po;
  2. import java.util.Date;
  3. /**
  4. * Created by shucheng on 2019-7-22 下午 22:43
  5. */
  6. public class Student_Nsf {
  7. private Integer id;
  8. private String name;
  9. private Date birthday; // 假设存的时间是精确到秒的
  10. public Student_Nsf() {
  11. }
  12. public Student_Nsf(Integer id, String name, Date birthday) {
  13. this.id = id;
  14. this.name = name;
  15. this.birthday = birthday;
  16. }
  17. // 省略setter、getter方法
  18. }

Student_Jks.java(针对jackson情形的实体类):

  1. package com.kittycoder.test1.po;
  2. import com.fasterxml.jackson.annotation.JsonFormat;
  3. import java.util.Date;
  4. /**
  5. * Created by shucheng on 2019-7-22 下午 22:43
  6. */
  7. public class Student_Jks {
  8. private Integer id;
  9. private String name;
  10. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  11. private Date birthday; // 假设存的时间是精确到秒的
  12. public Student_Jks() {
  13. }
  14. public Student_Jks(Integer id, String name, Date birthday) {
  15. this.id = id;
  16. this.name = name;
  17. this.birthday = birthday;
  18. }
  19. // 省略setter、getter方法
  20. }

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.kittycoder</groupId>
  7. <artifactId>JSONLibDemo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <!-- net.sf -->
  12. <net.sf.version>2.1</net.sf.version>
  13. <!-- fastjson -->
  14. <fastjson.version>1.2.47</fastjson.version>
  15. <!-- jackson -->
  16. <jackson.version>2.4.1</jackson.version>
  17. <!-- junit -->
  18. <junit.version>4.12</junit.version>
  19. </properties>
  20. <dependencies>
  21. <!-- net.sf json -->
  22. <dependency>
  23. <groupId>net.sf.json-lib</groupId>
  24. <artifactId>json-lib</artifactId>
  25. <version>${net.sf.version}</version>
  26. <classifier>jdk15</classifier>
  27. </dependency>
  28. <!-- Alibaba Fastjson -->
  29. <dependency>
  30. <groupId>com.alibaba</groupId>
  31. <artifactId>fastjson</artifactId>
  32. <version>${fastjson.version}</version>
  33. </dependency>
  34. <!-- jackson相关包 -->
  35. <dependency>
  36. <groupId>com.fasterxml.jackson.core</groupId>
  37. <artifactId>jackson-core</artifactId>
  38. <version>${jackson.version}</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>com.fasterxml.jackson.core</groupId>
  42. <artifactId>jackson-annotations</artifactId>
  43. <version>${jackson.version}</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>com.fasterxml.jackson.core</groupId>
  47. <artifactId>jackson-databind</artifactId>
  48. <version>${jackson.version}</version>
  49. </dependency>
  50. <!-- 单元测试 -->
  51. <dependency>
  52. <groupId>junit</groupId>
  53. <artifactId>junit</artifactId>
  54. <version>${junit.version}</version>
  55. <scope>test</scope>
  56. </dependency>
  57. </dependencies>
  58. </project>

参考链接:

https://blog.csdn.net/qq_41570658/article/details/89383110

https://blessht.iteye.com/blog/2018901

https://www.cnblogs.com/wangshen31/p/8961691.html

发表评论

表情:
评论列表 (有 0 条评论,587人围观)

还没有评论,来说两句吧...

相关阅读