美文网首页
Gson报错:com.google.gson.JsonSynta

Gson报错:com.google.gson.JsonSynta

作者: 曼昱的小蓝毛巾 | 来源:发表于2021-03-22 14:09 被阅读0次

1、报错信息:

com.google.gson.JsonSyntaxException: 1616389668000
    at 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);

相关文章

网友评论

      本文标题:Gson报错:com.google.gson.JsonSynta

      本文链接:https://www.haomeiwen.com/subject/supzcltx.html