gpt4 book ai didi

java - 将 Gson 与 Date4J 日期时间一起使用

转载 作者:行者123 更新时间:2023-11-29 03:29:57 25 4
gpt4 key购买 nike

我想反序列化以下 JSON

   {
"daysFree":
[
"2013-10-01T00:00:00",
"2013-10-02T00:00:00",
"2013-10-03T00:00:00",
"2013-10-04T00:00:00",
"2013-10-05T00:00:00",
"2013-10-06T00:00:00",
"2013-10-07T00:00:00",
"2013-10-08T00:00:00",
"2013-10-09T00:00:00",
"2013-10-10T00:00:00",
"2013-10-11T00:00:00",
"2013-10-12T00:00:00",
"2013-10-13T00:00:00",
"2013-10-28T00:00:00",
"2013-10-29T00:00:00",
"2013-10-30T00:00:00",
"2013-10-31T00:00:00"
],
"daysNoInfo":
[
],
"daysReserved":
[
"2013-10-14T00:00:00",
"2013-10-15T00:00:00",
"2013-10-16T00:00:00",
"2013-10-17T00:00:00",
"2013-10-18T00:00:00",
"2013-10-19T00:00:00",
"2013-10-20T00:00:00",
"2013-10-21T00:00:00",
"2013-10-22T00:00:00",
"2013-10-23T00:00:00",
"2013-10-24T00:00:00",
"2013-10-25T00:00:00",
"2013-10-26T00:00:00",
"2013-10-27T00:00:00"
],
"month": 10,
"year": 2013
}

这个类:

package xy;

import hirondelle.date4j.DateTime;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class OneMonth {

private List<DateTime> daysFree = new ArrayList<DateTime>();
private List<DateTime> daysNoInfo = new ArrayList<DateTime>();
private List<DateTime> daysReserved = new ArrayList<DateTime>();
private Integer month;
private Integer year;

public List<DateTime> getDaysFree() {
return daysFree;
}

public void setDaysFree(List<DateTime> daysFree) {
this.daysFree = daysFree;
}

public List<DateTime> getDaysNoInfo() {
return daysNoInfo;
}

public void setDaysNoInfo(List<DateTime> daysNoInfo) {
this.daysNoInfo = daysNoInfo;
}

public List<DateTime> getDaysReserved() {
return daysReserved;
}

public void setDaysReserved(List<DateTime> daysReserved) {
this.daysReserved = daysReserved;
}

public Integer getMonth() {
return month;
}

public void setMonth(Integer month) {
this.month = month;
}

public Integer getYear() {
return year;
}

public void setYear(Integer year) {
this.year = year;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

}

我试过

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
OneMonth m = gson.fromJson(jsonString, OneMonth.class);

但这失败了

09-13 14:18:44.287: E/DAY(31821): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 57

可能是因为 Gson 不“知道”hirondelle.date4j.DateTime 类。

当我使用 java.ultil.Date 而不是 hirondelle.date4j.DateTime 时,一切正常。

我能否以某种方式让 Gson 与该类一起工作?

最佳答案

我为 DateTime 类编写了一个 TypeAdapter。我向您提供我的结果。

package stackoverflow.questions.q18786243;

import hirondelle.date4j.DateTime;

import java.io.IOException;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.*;


public final class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return typeToken.getRawType() == java.sql.Date.class
? (TypeAdapter<T>) new DateTimeTypeAdapter() : null;
}
};

@Override
public synchronized DateTime read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}

String s = in.nextString();
return new DateTime(s);

}

@Override
public synchronized void write(JsonWriter out, DateTime value) throws IOException {
out.value(value == null ? null : value.toString());
}
}

调用方式如下:

GsonBuilder g = new GsonBuilder();
g.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter());
Gson gson = g.create();
OneMonth m = gson.fromJson(s, OneMonth.class);
System.out.println(m);

结果是:

OneMonth [daysFree=[2013-10-01T00:00:00, 2013-10-02T00:00:00, 2013-10-03T00:00:00, 
2013-10-04T00:00:00, 2013-10-05T00:00:00, 2013-10-06T00:00:00,
2013-10-07T00:00:00, 2013-10-08T00:00:00, 2013-10-09T00:00:00, 2013-10-10T00:00:00,
2013-10-11T00:00:00, 2013-10-12T00:00:00, 2013-10-13T00:00:00, 2013-10-28T00:00:00,
2013-10-29T00:00:00, 2013-10-30T00:00:00, 2013-10-31T00:00:00],
daysNoInfo=[], daysReserved=[2013-10-14T00:00:00, 2013-10-15T00:00:00,
2013-10-16T00:00:00, 2013-10-17T00:00:00, 2013-10-18T00:00:00, 2013-10-19T00:00:00,
2013-10-20T00:00:00, 2013-10-21T00:00:00, 2013-10-22T00:00:00, 2013-10-23T00:00:00,
2013-10-24T00:00:00, 2013-10-25T00:00:00, 2013-10-26T00:00:00, 2013-10-27T00:00:00],
month=10, year=2013]

关于java - 将 Gson 与 Date4J 日期时间一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18786243/

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