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

4

TypeAdapterDateTime班级写了一篇。我给你我的结果。

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]
于 2013-09-20T23:01:18.810 回答