我想反序列化以下 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 与该课程一起工作吗?