一、先写一个测试用的Java实体
代码如下:
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
/**
* 测试用户类
*
* @author:gan
* @date: 2023-04-28 15:33
*/
@Data
public class User {
private String name;
private Integer age;
private Date birthday;
private char sex;
private Float height;
private Double weight;
private BigDecimal money;
private Boolean adult;
//下面这些初始化为空,用于后面测试效果时用
private String nameDesc;
private Integer ageDesc;
private Date birthdayDesc;
private char sexDesc;
private Float heightDesc;
private Double weightDesc;
private BigDecimal moneyDesc;
private Boolean adultDesc;
public User() {
}
public User(String name, Integer age, Date birthday, char sex, Float height, Double weight, BigDecimal money, Boolean adult) {
this.name = name;
this.age = age;
this.birthday = birthday;
this.sex = sex;
this.height = height;
this.weight = weight;
this.money = money;
this.adult = adult;
}
}
初始化并输出一下:
public static void main(String[] args) {
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
}
注意,如果没有使用“@Data”注解,一定要加上get和set方法,不然转化之后的Json是空的!
二、将Java实体转化为Json
1、基于com.alibaba.fastjson.JSON
主要是对SerializerFeature枚举类的使用,详细的可以看这个博客:
https://blog.csdn.net/xiang__liu/article/details/81570923
或者看这里:
https://www.javadoc.io/static/com.alibaba/fastjson/1.2.2/com/alibaba/fastjson/serializer/SerializerFeature.html#BeanToArray
下面是对SerializerFeature的使用演示:
(1)、JSON.toJSONString(user),会丢掉值为空的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
运行结果如下:
可以看到,直接使用toJSONString将Java实体转化为Json,会把没有显示初始化的字段丢掉。
“\u0000”是char类型的默认值,相对于null。
(2)、JSON.toJSONString(user, SerializerFeature.WriteMapNullValue),保留值为空的字段,并初始化为null。
加上“SerializerFeature.WriteMapNullValue”,会输出值为空的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
运行结果如下:
(3)、JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString),会将非空的字段值加上"",并丢掉没有初始化的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
运行结果如下:
(4)、JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty),会将String类型的字段加上"",没有显示初始化的String类型参数保留,保留默认初始化为null的字段,同时丢掉其他未初始化的、非String类型的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
运行结果如下:
(5)、JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty),会非空、String类型的字段加上"",保留默认初始化为null的字段,同时丢掉其他未初始化的、非String类型的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
运行结果如下:
(6)、JSON.toJSONString(user, SerializerFeature.QuoteFieldNames),转化之后的字段名会加上"",和默认一样。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
运行结果如下:
(7)、JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat),转化之后Date类型为传入格式,默认是时间戳。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
String jsonStr7 = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonStr7:" + jsonStr7);
运行结果如下:
(8)、JSON.toJSONString(user, SerializerFeature.WriteNullNumberAsZero),转化之后会将没有初始化的数字类型(BigDecimal、Float、Double)字段初始化为0,保留初始化为null的字段,丢掉其他类型未显示初始化的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
String jsonStr7 = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonStr7:" + jsonStr7);
String jsonStr8 = JSON.toJSONString(user, SerializerFeature.WriteNullNumberAsZero);
System.out.println("jsonStr8:" + jsonStr8);
运行结果如下:
(9)、JSON.toJSONString(user, SerializerFeature.WriteNullBooleanAsFalse),转化之后会将没有初始化的Boolean类型字段初始化为false,保留初始化为null的字段,丢掉其他类型未显示初始化的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
String jsonStr7 = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonStr7:" + jsonStr7);
String jsonStr8 = JSON.toJSONString(user, SerializerFeature.WriteNullNumberAsZero);
System.out.println("jsonStr8:" + jsonStr8);
String jsonStr9 = JSON.toJSONString(user, SerializerFeature.WriteNullBooleanAsFalse);
System.out.println("jsonStr9:" + jsonStr9);
运行结果如下:
(10)、JSON.toJSONString(user, SerializerFeature.PrettyFormat),格式化Json,丢掉其他未显示初始化的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
String jsonStr7 = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonStr7:" + jsonStr7);
String jsonStr8 = JSON.toJSONString(user, SerializerFeature.WriteNullNumberAsZero);
System.out.println("jsonStr8:" + jsonStr8);
String jsonStr9 = JSON.toJSONString(user, SerializerFeature.WriteNullBooleanAsFalse);
System.out.println("jsonStr9:" + jsonStr9);
String jsonStr10 = JSON.toJSONString(user, SerializerFeature.PrettyFormat);
System.out.println("jsonStr10:" + jsonStr10);
运行结果如下:
2、基于com.alibaba.fastjson.JSONObject
JSONObject是JSON的子类,并且没有重写“toJSONString”方法,所以效果是一样的。
代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSONObject.toJSONString start================================");
String jsonObjectStr1 = JSONObject.toJSONString(user);
System.out.println("jsonObjectStr1:" + jsonObjectStr1);
String jsonObjectStr2 = JSONObject.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonObjectStr2:" + jsonObjectStr2);
String jsonObjectStr3 = JSONObject.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonObjectStr3:" + jsonObjectStr3);
String jsonObjectStr4 = JSONObject.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonObjectStr4:" + jsonObjectStr4);
String jsonObjectStr5 = JSONObject.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonObjectStr5:" + jsonObjectStr5);
String jsonObjectStr6 = JSONObject.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonObjectStr6:" + jsonObjectStr6);
String jsonObjectStr7 = JSONObject.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonObjectStr7:" + jsonObjectStr7);
String jsonObjectStr8 = JSONObject.toJSONString(user, SerializerFeature.WriteNullNumberAsZero);
System.out.println("jsonObjectStr8:" + jsonObjectStr8);
String jsonObjectStr9 = JSONObject.toJSONString(user, SerializerFeature.WriteNullBooleanAsFalse);
System.out.println("jsonObjectStr9:" + jsonObjectStr9);
String jsonObjectStr10 = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat);
System.out.println("jsonObjectStr10:" + jsonObjectStr10);
System.out.println("=================================JSONObject.toJSONString end================================");
3、基于com.fasterxml.jackson.databind.ObjectMapper的writeValueAsString方法
代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
ObjectMapper objectMapper = new ObjectMapper();
try {
String objectMapperStr = objectMapper.writeValueAsString(user);
System.out.println("objectMapperStr:" + objectMapperStr);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
运行结果如下:
writeValueAsString方法会保留没有显示初始化的的字段,除了char类型初始化值为“\u0000”,其他都为“null”。
4、基于com.google.gson.Gson的toJson方法。
(1)、直接实例化一个Gson,将Java实体传入实例中的toJson,会丢掉没有初始化的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
Gson gson = new Gson();
String gsonStr1 = gson.toJson(user);
System.out.println("gsonStr1:" + gsonStr1);
运行结果如下:
char类型还是保留下来了。
(2)、在转化为json的同时,保留没有显示初始化的字段。
“Gson gson = new Gson();”只能简单地将Java实体转为json,要保留没有显示初始化的字段,需要用到“GsonBuilder”初始化“Gson”,代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
Gson gson = new GsonBuilder()
.serializeNulls()
.create();
String gsonStr1 = gson.toJson(user);
System.out.println("gsonStr1:" + gsonStr1);
运行结果如下:
可以看到没有显示初始化的字段都保留下来了,并且值都为“null”。
(3)、设置时间格式。
上面可以看到时间格式默认是国际化格式,想要转换为“yyyy年MM月dd日 时:分:秒”怎么办?可以使用“setDateFormat”格式化时间,代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
Gson gson = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy年MM月dd日 HH:mm:ss")
.create();
String gsonStr1 = gson.toJson(user);
System.out.println("gsonStr1:" + gsonStr1);
运行结果如下:
可以看到时间已经转换为目标格式。
(4)、格式化输出。
如果觉得转换之后的json只有一行不好看,可以使用“setPrettyPrinting”格式化json的格式,代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
Gson gson = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy年MM月dd日 HH:mm:ss")
.setPrettyPrinting()
.create();
String gsonStr1 = gson.toJson(user);
System.out.println("gsonStr1:" + gsonStr1);
运行结果如下:
三、将Json转换为Java实体
1、先拿到测试Json
拿到测试用的Java实体转化为Json,格式化后如下:
{
"adult": true,
"adultDesc": null,
"age": 25,
"ageDesc": null,
"birthday": 908169922000,
"birthdayDesc": null,
"height": 180.0,
"heightDesc": null,
"money": 2104.5,
"moneyDesc": null,
"name": "王宇",
"nameDesc": null,
"sex": "男",
"sexDesc": "\u0000",
"weight": 71.55,
"weightDesc": null
}
2、基于com.alibaba.fastjson.JSON
使用JSON的parseObject(String text, Class clazz)方法可以直接将Json字符转化为对应clazz的Java实体,代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
User user = JSON.parseObject(userJson, User.class);
System.out.println("user:" + user);
运行结果如下:
3、基于com.alibaba.fastjson.JSONObject
JSONObject中转化Java实体的方法parseObject也是来自JSON,并且没有重写,效果是一样的,代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
User user = JSONObject.parseObject(userJson, User.class);
System.out.println("user:" + user);
运行结果如下:
4、基于com.fasterxml.jackson.databind.ObjectMapper的readValue方法
需要先实例化ObjectMapper,再调用readValue方法,代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
ObjectMapper objectMapper = new ObjectMapper();
try {
User user = objectMapper.readValue(userJson, User.class);
System.out.println("user:" + user);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
运行结果如下:
5、基于com.google.gson.Gson
Gson实例化后,使用fromJson方法将Json转化为Java实体,代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
Gson gson = new Gson();
User user = gson.fromJson(userJson, User.class);
System.out.println("user:" + user);
运行结果如下:
可以看到时间戳转Date类型报错了。
需要使用GsonBuilder进行实例化,在适配一下Date类型的字段。代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
})
.create();
User user = gson.fromJson(userJson, User.class);
System.out.println("user:" + user);
再次运行:文章来源:https://www.toymoban.com/news/detail-657812.html
文章来源地址https://www.toymoban.com/news/detail-657812.html
到了这里,关于Java实体和JSON之间的相互转换的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!