1

次の 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をそのクラスで動作させることはできますか?

4

1 に答える 1