java - GSON 到 JSON 的转换——长格式和简单日期格式“yyyy-MM-dd'T'HH:mm'Z'”
问题描述
我正在使用 GSON 将 JSONArray 转换为 Java 对象。我需要将长日期和简单日期格式“yyyy-MM-dd'T'HH:mm'Z'”都转换为 Java 对象。我可以单独转换长日期或简单日期,但不能同时转换两者。
使用下面的代码片段进行长转换:
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
return new Date(json.getAsJsonPrimitive()
.getAsLong());
}
});
Gson gson = builder.create();
Type userListType = new TypeToken<ArrayList<CurrentLocation>>() {
}.getType();
gson.fromJson(data, userListType);
我需要转换的示例数据:
[
{
"instance": {
"location": {
"lat": 31.522291,
"lon": -96.532816,
"timestamp": 1587693720000
},
"timeAtLocation": "2020-04-23T04:59Z"
}
},
{
"instance": {
"location": {
"lat": 31.522291,
"lon": -96.532816,
"timestamp": 1737693720000
},
"timeAtLocation": "2020-0-23T04:59Z"
}
}
]
解决方案
请尝试以下代码:
public class Main {
static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'";
static final Calendar calendar = Calendar.getInstance();
static class LongUtil {
static boolean isLong(String longValue) {
try {
Long.parseLong(longValue);
return true;
} catch (RuntimeException e) {
return false;
}
}
}
static class DateDeserializer implements JsonDeserializer<Date> {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
@Override
public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
System.out.println(type.getTypeName());
System.out.println(jsonElement.getAsJsonPrimitive());
if (jsonElement.getAsJsonPrimitive() != null) {
final String expectedDateValue = jsonElement.getAsJsonPrimitive().getAsString();
if (LongUtil.isLong(expectedDateValue)) {
System.out.println("It is long value hence parsing it to long");
calendar.setTimeInMillis(Long.parseLong(expectedDateValue));
return calendar.getTime();
} else {
System.out.println("It is dateformat value hence parsing it to dateformat");
try {
return simpleDateFormat.parse(expectedDateValue);
} catch (ParseException e) {
throw new JsonParseException("Invalud dateFormat while parsing");
}
}
} else {
throw new JsonParseException("JSOn Premitive Exception null");
}
}
}
public static void main(String[] args) throws IOException {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = builder.create();
Type userListType = new TypeToken<ArrayList<Wrapper>>() {
}.getType();
File file = new File("response.json");
System.out.println("file exists : " + file.exists());
JsonReader jsonReader = new JsonReader(new FileReader(file));
List<Wrapper> wrapper = gson.fromJson(jsonReader, userListType);
System.out.println(wrapper);
}
class Wrapper {
User instance;
@Override
public String toString() {
return "Wrapper{" +
"instance=" + instance +
'}';
}
}
class User {
Location location;
Date timeAtLocation;
@Override
public String toString() {
return "User{" +
"location=" + location +
", timeAtLocation='" + timeAtLocation + '\'' +
'}';
}
}
class Location {
String lat;
String lon;
Date timestamp;
@Override
public String toString() {
return "Location{" +
"lat='" + lat + '\'' +
", lon='" + lon + '\'' +
", timestamp='" + timestamp + '\'' +
'}';
}
}
}
解释:
当deserializing
我检查输入字符串是否在dateformat
或long
基于它创建日历实例时,我设置了 long 并最终获取日期对象日期,否则只需使用字符串并根据日期格式化程序对其进行格式化。
输出:
file exists : true
It is long value hence parsing it to long
It is dateformat value hence parsing it to dateformat
It is long value hence parsing it to long
It is dateformat value hence parsing it to dateformat
[Wrapper{instance=User{location=Location{lat='31.522291', lon='-96.532816', timestamp='Fri May 01 13:49:08 IST 2020'}, timeAtLocation='Thu Apr 23 04:59:00 IST 2020'}}, Wrapper{instance=User{location=Location{lat='31.522291', lon='-96.532816', timestamp='Fri Jan 24 10:12:00 IST 2025'}, timeAtLocation='Mon Dec 23 04:59:00 IST 2019'}}]
推荐阅读
- java - 无法从另一个 docker 容器连接到 docker 容器中的 mysql
- process - 创建进程或线程哪个更快?为什么?
- python - Python - 拆分列表以获取最小值和最大值
- python - 运行软件源导致 Python 错误 (LMDE 3)
- c# - 扩展 Windows 10 中音量变化的音量控制(类似于 Windows 上的 Spotify)
- node.js - 两个不同密码的 .env 环境
- spring-boot - Spring 和 Kafka:对 Kafka Producer 和 Kafka Streams 使用相同的绑定
- flutter - 如何在颤动中更改我的平面按钮的位置......(从最左边到最右边)
- java - 查找与用户输入匹配的数组中的第一个元素并在 2 个二维数组之间打印该位置
- javascript - 如何使用模态对话框库将用户输入存储为变量?(JavaScript)