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!

有帮助吗?

解决方案

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;
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top