简体   繁体   中英

Always getting value of date field as current date after converting json into java object using JSONSerializer.toJava

I am converting the following json into java object using JSONSerializer.toJava.

{
    "sessionId": "d792-54fd8a87-ses-Administrator-2200-0",
    "campaignId": 2,
    "callBackTime": "2015-08-08 07:23:00",
    "isSelfCallBack": "false",
    "userId": "a1",
    "callBackHandlerType": "voice.campaign.callback.handler",
    "callBackProperties": 
     {
         "customerId": "112",
        "phone": "33334444"
     }
}

And my root class for json config is described as below

public class ProxyAddCallbackRequestBean extends ProxySessionRequestBean {

    private static final long serialVersionUID = 1L;

    private Integer campaignId;
    private Date callBackTime;
    private boolean isSelfCallBack;
    private String userId;
    private String callBackHandlerType;
    private Map<String, String> callBackProperties;

    public Integer getCampaignId() {
        return campaignId;
    }

    public void setCampaignId(Integer campaignId) {
        this.campaignId = campaignId;
    }

    public Date getCallBackTime() {
        return callBackTime;
    }

    public void setCallBackTime(Date callBackTime) {
        this.callBackTime = callBackTime;
    }

    public boolean isSelfCallBack() {
        return isSelfCallBack;
    }

    public void setSelfCallBack(boolean isSelfCallBack) {
        this.isSelfCallBack = isSelfCallBack;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getCallBackHandlerType() {
        return callBackHandlerType;
    }

    public void setCallBackHandlerType(String callBackHandlerType) {
        this.callBackHandlerType = callBackHandlerType;
    }

    public Map<String, String> getCallBackProperties() {
        return callBackProperties;
    }

    public void setCallBackProperties(Map<String, String> callBackProperties) {
        this.callBackProperties = callBackProperties;
    }

}

After converting to java object, callBackTime value is set to current time while other fields have correct values.

I am new to json can you please help me to find out where i am doing wrong.

Assuming you're using this json-lib , there's nothing from a quick scan of the documentation to suggest that it will auto-convert a String to a Date. Therefore, you're going to need to parse the date. If you're happy pulling in the dependency Joda Time has a good reputation. Otherwise, if the date you've shown is expected, something like:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public void setCallBackTime(String rawTime) {
    this.callBackTime = df.parse(rawTime);
}

should get you started. (Javadoc for SimpleDateFormat )

(Note that the date you've quoted looks like, but isn't ISO 8601).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM