Domanda

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!

È stato utile?

Soluzione

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;
    }
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top