使用 json-lib 解析日期

发布于 2024-12-29 12:46:11 字数 389 浏览 3 评论 0原文

我有以下 JSON 对象:

{“startDate”:“30/01/2008”,“startPeriod”:“2008”,“dboid”:“5308204301485575800000”,“action”:“更新”,“gri d":"variantAssigGrid","endDate":"30/01/2011","endPeriod":"2011","机构":"5301004301485575300000"}

应用 JSONObject.toBean 后,开始日期和结束日期将设置为结果 bean 中的当前系统日期(而不是 json 字符串中的值)。看起来它们是用 new Date() 初始化的。

有没有办法指定日期格式?我研究了 JsonConfig 类,但没有取得太大成功。

提前致谢!

I have the following JSON object:

{"startDate":"30/01/2008","startPeriod":"2008","dboid":"5308204301485575800000","action":"update","grid":"variantAssigGrid","endDate":"30/01/2011","endPeriod":"2011","institution":"5301004301485575300000"}

After applying JSONObject.toBean, start and end date are set to the current system date in the resulting bean (instead of the values in the json string). It looks like they are initialized with new Date().

Is there any way of specifying the date format ? I looked into JsonConfig class without much success.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮世清欢 2025-01-05 12:46:11

注册您自己的处理器...

this.jsonConfigToJSON = new JsonConfig();
this.jsonConfigToJSON.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessor() {
    @Override
    public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
        return process(value, jsonConfig);
    }

    @Override
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        return process(value, jsonConfig);
    }

    private Object process(Object value, JsonConfig jsonConfig) {
        // For Unix Time
        return ((Date) value).getTime() / 1000L;
    }
});

Register your own processor...

this.jsonConfigToJSON = new JsonConfig();
this.jsonConfigToJSON.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessor() {
    @Override
    public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
        return process(value, jsonConfig);
    }

    @Override
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        return process(value, jsonConfig);
    }

    private Object process(Object value, JsonConfig jsonConfig) {
        // For Unix Time
        return ((Date) value).getTime() / 1000L;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文