如何在不将 LocalDateTime 字段转换为扩展的 json 对象的情况下将 java 对象转换为简单的 json 字符串?
How to convert a java object to simple json string without converting the LocalDateTime field to an extended json object?
我需要帮助将 java 对象转换为 json 字符串,而不将 LocalDateTime 字段转换为单独的对象。
class MyObj {
LocalDateTime date;
}
那么,
MyObj dateObj = new MyObj();
dateObj.date = LocalDateTime.now();
当我将其转换为 json、
Gson gson = new GsonBuilder().setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();
gson.toJson(dateObj);
我得到这个:
{
"date": {
"date": {
"year": 2020,
"month": 8,
"day": 27
},
"time": {
"hour": 8,
"minute": 59,
"second": 47,
"nano": 0
}
}
}
但我想要这个:
"date" : "2020-08-27T08:59:470Z"
请帮我一下。
根据 javadoc 的 setDateFormat
The date format will be used to serialize and deserialize {@link java.util.Date}, {@link java.sql.Timestamp} and {@link java.sql.Date}.
您需要为 LocalDateTime.class
注册自定义序列化程序
JsonSerializer<LocalDateTime> localDateTimeSerializer = (src, type, context) -> {
String date = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").format(src);
// String date = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(src);
// String date = src.toString();
return new JsonPrimitive(date);
};
Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, localDateTimeSerializer).create();
gson.toJson(dateObj);
注意: LocalDateTime 不存储时区信息,因此您实际上不能在日期中使用 Z
格式模式。
我是这样解决的
创建 Gson 对象:
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
.create();
使用方法如下:
String jsonRequestString = gson.toJson(request);
创建序列化程序:
class LocalDateAdapter implements JsonSerializer<LocalDate> {
@Override
public JsonElement serialize(LocalDate date, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
}
我需要帮助将 java 对象转换为 json 字符串,而不将 LocalDateTime 字段转换为单独的对象。
class MyObj {
LocalDateTime date;
}
那么,
MyObj dateObj = new MyObj();
dateObj.date = LocalDateTime.now();
当我将其转换为 json、
Gson gson = new GsonBuilder().setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();
gson.toJson(dateObj);
我得到这个:
{
"date": {
"date": {
"year": 2020,
"month": 8,
"day": 27
},
"time": {
"hour": 8,
"minute": 59,
"second": 47,
"nano": 0
}
}
}
但我想要这个:
"date" : "2020-08-27T08:59:470Z"
请帮我一下。
根据 javadoc 的 setDateFormat
The date format will be used to serialize and deserialize {@link java.util.Date}, {@link java.sql.Timestamp} and {@link java.sql.Date}.
您需要为 LocalDateTime.class
JsonSerializer<LocalDateTime> localDateTimeSerializer = (src, type, context) -> {
String date = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").format(src);
// String date = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(src);
// String date = src.toString();
return new JsonPrimitive(date);
};
Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, localDateTimeSerializer).create();
gson.toJson(dateObj);
注意: LocalDateTime 不存储时区信息,因此您实际上不能在日期中使用 Z
格式模式。
我是这样解决的
创建 Gson 对象:
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
.create();
使用方法如下:
String jsonRequestString = gson.toJson(request);
创建序列化程序:
class LocalDateAdapter implements JsonSerializer<LocalDate> {
@Override
public JsonElement serialize(LocalDate date, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
}