开发者社区> 问答> 正文

关于Gson解析时间时的问题 有大神吗??

用Gson解析时遇到这个问题

com.google.gson.JsonSyntaxException: 1461689198000
04-27 00:46:51.159 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:74)
04-27 00:46:51.159 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:59)
04-27 00:46:51.159 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:41)
04-27 00:46:51.159 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:116)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:116)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.Gson.fromJson(Gson.java:879)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.Gson.fromJson(Gson.java:844)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.Gson.fromJson(Gson.java:793)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.yurikami.lib.net.parser.impl.ModelParser.parse(ModelParser.java:48)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.yurikami.lib.net.UICallBack.onResponse(UICallBack.java:54)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at java.lang.Thread.run(Thread.java:818)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: Caused by: java.text.ParseException: Failed to parse date ["1461689198000']: Invalid time zone indicator '9' (at offset 0)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274)
04-27 00:46:51.160 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:72)
04-27 00:46:51.166 17190-20868/link.ebbinghaus.planning W/System.err: ... 16 more
04-27 00:46:51.166 17190-20868/link.ebbinghaus.planning W/System.err: Caused by: java.lang.IndexOutOfBoundsException: Invalid time zone indicator '9'
04-27 00:46:51.166 17190-20868/link.ebbinghaus.planning W/System.err: at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:245)
04-27 00:46:51.166 17190-20868/link.ebbinghaus.planning W/System.err: ... 17 more

展开
收起
爵霸 2016-02-29 16:44:17 7205 0
1 条回答
写回答
取消 提交回答
  • 表格存储(TableStore)技术负责人,专注结构化数据存储领域技术和解决方案。

    看了下DateTypeAdaptor的代码,如下:
    private synchronized Date deserializeToDate(String json) {

    try {
      return localFormat.parse(json);
    } catch (ParseException ignored) {
    }
    try {
      return enUsFormat.parse(json);
    } catch (ParseException ignored) {
    }
    try {
        return ISO8601Utils.parse(json, new ParsePosition(0));
    } catch (ParseException e) {
      throw new JsonSyntaxException(json, e);
    }

    }

    它只解析localFormat,enUsFormat或ISO8601三个标准定义的日志字符串的解析,不支持整型。

    解决方案有三种:

    1. Json内1461689198000这个值按它的规范变为日期字符串
    2. Date类型改为Long类型,来解析1461689198000这个值,然后在你的类内自己转换成Date
    3. 自定义TypeAdaptor
    2019-07-17 18:50:33
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像