سؤال

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