您需要为 Dates 注册自己的反序列化器。
我在下面创建了一个小示例,其中 JSON 字符串“23-11-2010 10:00:00”被反序列化为 Date 对象:
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class Dummy {
private Date date;
/**
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
public static void main(String[] args) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String date = json.getAsJsonPrimitive().getAsString();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
});
Gson gson = builder.create();
String s = "{\"date\":\"23-11-2010 10:00:00\"}";
Dummy d = gson.fromJson(s, Dummy.class);
System.out.println(d.getDate());
}
}