gpt4 book ai didi

java - Gson时间反序列化

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:03:30 26 4
gpt4 key购买 nike

我有一个 JSON 字符串,我想使用 Gson 反序列化 - {"Id":3,"Title":"Roskilde","Description":"Famous Danske festival","StartingTime":"2016 -06-12T00:00:00","持续时间":"02:02:00"尝试反序列化 Duration 字段时出现的错误:

Unparseable date: "02:02:00"

反序列化器(我的想法是添加两种可能的日期反序列化格式):

Gson gSon=  new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer()).create();


private static final String[] DATE_FORMATS = new String[] {
"yyyy-MM-dd'T'HH:mm:ss",
"HH:mm:ss"
};


private class DateDeserializer implements JsonDeserializer<Date> {

@Override
public Date deserialize(JsonElement jsonElement, Type typeOF,
JsonDeserializationContext context) throws JsonParseException {
for (String format : DATE_FORMATS) {
try {
return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString());
} catch (ParseException e) {
}
}
throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: " + Arrays.toString(DATE_FORMATS));
}
}

和我的事件类(如您所见,“Duration”不是日期类型 - 它是时间类型 - 我应该怎么做才能使反序列化器将 Duration 读取为时间类型而不是日期?

    private int Id;
private String Title;
private String Description;
private Date StartingTime;
private Time Duration;
public Event(int id, String title,String description, String place, Date startingTime, Time duration)
{
this.Id = id;
this.Description = description;
this.Title = title;
this.StartingTime = startingTime;
this.Duration = duration;
}

最佳答案

为时间反序列化添加另一个类 - 确保将返回值转换为“时间”。(另请参阅:cast a String to sql time)

    private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
private static final String TIME_FORMAT = "HH:mm:ss";

private class DateDeserializer implements JsonDeserializer<Date> {

@Override
public Date deserialize(JsonElement jsonElement, Type typeOF,
JsonDeserializationContext context) throws JsonParseException {
try {
return new SimpleDateFormat(DATE_FORMAT, Locale.US).parse(jsonElement.getAsString());
} catch (ParseException e) {
}

throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: " + DATE_FORMAT);
}
}

private class TimeDeserializer implements JsonDeserializer<Time> {

@Override
public Time deserialize(JsonElement jsonElement, Type typeOF,
JsonDeserializationContext context) throws JsonParseException {
try {

String s = jsonElement.getAsString();
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.US);
sdf.parse(s);
long ms = sdf.parse(s).getTime();
Time t = new Time(ms);
return t;
} catch (ParseException e) {
}
throw new JsonParseException("Unparseable time: \"" + jsonElement.getAsString()
+ "\". Supported formats: " + TIME_FORMAT);
}
}

注册两个类:

    GsonBuilder gSonBuilder=  new GsonBuilder();
gSonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
gSonBuilder.registerTypeAdapter(Time.class, new TimeDeserializer());
Gson gSon = gSonBuilder.create();

关于java - Gson时间反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com