我想反序列化以下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与该课程合作吗?
答案 0 :(得分:4)
我为TypeAdapter
课写了DateTime
。我向你提供了我的结果。
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]