当前位置: 代码迷 >> 综合 >> Gson报错:com.google.gson.JsonSyntaxException: 1616389668000
  详细解决方案

Gson报错:com.google.gson.JsonSyntaxException: 1616389668000

热度:31   发布时间:2023-11-10 22:28:14.0

1、报错信息:

com.google.gson.JsonSyntaxException: 1616389668000at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:127)at com.google.gson.DefaultDateTypeAdapter.read(DefaultDateTypeAdapter.java:103)at com.google.gson.DefaultDateTypeAdapter.read(DefaultDateTypeAdapter.java:40)at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)at com.google.gson.Gson.fromJson(Gson.java:888)at com.google.gson.Gson.fromJson(Gson.java:853)at com.google.gson.Gson.fromJson(Gson.java:802)at com.google.gson.Gson.fromJson(Gson.java:774)at cn.ubitech.ttc.common.Util.fromJson2List(Util.java:1345)Caused by: java.text.ParseException: Failed to parse date ["1616389668000']: Invalid time zone indicator '6'at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274)at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:125)... 96 more
Caused by: java.lang.IndexOutOfBoundsException: Invalid time zone indicator '6'at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:245)... 97 more

2、原因

Gson无法解析位数太长的时间格式

3、解决方法

GsonBuilder builder = new GsonBuilder();builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    return new Date(json.getAsJsonPrimitive().getAsLong());}});Gson gson = builder.setDateFormat("yyyy-MM-dd HH:mm:ss").create();T[] list = gson.fromJson(json, type);return Arrays.asList(list);
  相关解决方案