简体   繁体   English

使用JSONSerializer.toJava将json转换为java对象后,总是获取date字段的值作为当前日期

[英]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. 我正在使用JSONSerializer.toJava将以下json转换为java对象。

{
    "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 我的json配置的根类描述如下

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. 转换为Java对象后,将callBackTime值设置为当前时间,而其他字段具有正确的值。

I am new to json can you please help me to find out where i am doing wrong. 我是JSON的新手,请您帮我找出我在哪里做错了。

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. 假设您使用的是json-lib ,则快速浏览文档并没有任何暗示它会将字符串自动转换为日期的信息。 Therefore, you're going to need to parse the date. 因此,您将需要解析日期。 If you're happy pulling in the dependency Joda Time has a good reputation. 如果您乐意加入依赖关系, Joda Time的声誉很好。 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 ) (Javadoc for SimpleDateFormat

(Note that the date you've quoted looks like, but isn't ISO 8601). (请注意,您所引用的日期看起来像,但不是ISO 8601)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Oracle Date字段对象转换为java Date - converting oracle Date field Object to java Date Java GSON JsonSerializer无法将模型的Date类型字段转换为Long值 - Java GSON JsonSerializer not convert Date type field of my model to Long value JSON序列化器<Date> () 在有 JsonSerializer 时不适用于 java.util.Date 字段<Timestamp> () - JsonSerializer<Date>() is not working for java.util.Date field when there is JsonSerializer<Timestamp>() 在Java中将JSON对象转换为util.Date - Converting JSON object to util.Date in Java Java 8获取具有偏移量的当前日期的字符串值 - java 8 getting string value for current date with offset Java在不创建新Date对象的情况下获取当前日期 - Java getting current date without creating a new Date object 为什么 Jackson ObjectMapper 给我这个错误,将 JSON 字段转换为日期 object? 无法从字符串反序列化 java.util.Date 类型的值 - Why Jackson ObjectMapper give me this error converting JSON field into a Date object? Can not deserialize value of type java.util.Date from String 将 adb shell 日期返回值转换为 Java 中的日期对象 - Converting adb shell date returned value to Date Object in Java 将Java日期转换为JSON DateTime - Converting Java Date to JSON DateTime 将人类可读的日期转换为Java Date对象 - Converting human readable date to java date object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM