Java 如何转换Jackson和Gson之间的日期?
在Spring配置的REST服务器中,我们使用Jackson将对象转换为Json。此对象包含几个java.util.Date对象 当我们尝试在Android设备上使用Gson的fromJson方法对其进行反序列化时,会得到一个“java.text.ParseException:Unparseable date”。自1970年以来,我们尝试将日期序列化为对应于毫秒的时间戳,但得到了相同的异常Java 如何转换Jackson和Gson之间的日期?,java,json,timestamp,gson,jackson,Java,Json,Timestamp,Gson,Jackson,在Spring配置的REST服务器中,我们使用Jackson将对象转换为Json。此对象包含几个java.util.Date对象 当我们尝试在Android设备上使用Gson的fromJson方法对其进行反序列化时,会得到一个“java.text.ParseException:Unparseable date”。自1970年以来,我们尝试将日期序列化为对应于毫秒的时间戳,但得到了相同的异常 是否可以将Gson配置为将时间戳格式的日期(如1291158000000)解析为java.util.dat
是否可以将Gson配置为将时间戳格式的日期(如1291158000000)解析为java.util.date对象?您需要注册自己的日期反序列化程序 我在下面创建了一个小示例,其中JSON字符串“23-11-2010 10:00:00”被反序列化为日期对象:
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());
}
}
import java.lang.reflect.Type;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入com.google.gson.JsonDeserializationContext;
导入com.google.gson.JsonDeserializer;
导入com.google.gson.JsonElement;
导入com.google.gson.JsonParseException;
公共类虚拟{
私人日期;
/**
*@param date要设置的日期
*/
公共作废设置日期(日期){
this.date=日期;
}
/**
*@返回日期
*/
公共日期getDate(){
返回日期;
}
公共静态void main(字符串[]args){
GsonBuilder=新的GsonBuilder();
registerTypeAdapter(Date.class,新的JsonDeserializer()){
@凌驾
公共日期反序列化(JsonElement json,类型typeOfT,JsonDeserializationContext)
抛出JsonParseException{
SimpleDataFormat=新的SimpleDataFormat(“dd-MM-yyy-HH:MM:ss”);
字符串日期=json.getAsJsonPrimitive().getAsString();
试一试{
返回格式.parse(日期);
}捕获(解析异常){
抛出新的运行时异常(e);
}
}
});
Gson Gson=builder.create();
字符串s=“{\'date\”:\“23-11-2010 10:00:00\”;
Dummy d=gson.fromJson(s,Dummy.class);
System.out.println(d.getDate());
}
}
关于Jackson,您不仅可以在数字(时间戳)和文本序列化(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS)之间进行选择,还可以定义用于文本变量的确切日期格式(SerializationConfig.setDateFormat)。因此,如果不支持杰克逊默认使用的ISO-8601格式,您应该能够强制使用Gson认可的内容
另外:如果你不介意在Gson上使用它,那么Jackson在Android上运行得很好。这并不能真正回答问题,是吗?问题明确指出“时间戳格式日期”