使用 JSONSerializer.toJava 将 json 转换为 java 对象后,始终将日期字段的值作为当前日期
Always getting value of date field as current date after converting 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"
}
}
我的根 class 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;
}
}
转换为 java 对象后,callBackTime
值设置为当前时间,而其他字段具有正确的值。
我是 json 的新手,你能帮我找出我做错的地方吗?
假设您使用的 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 具有良好的声誉。否则,如果您显示的日期是预期的,则类似于:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void setCallBackTime(String rawTime) {
this.callBackTime = df.parse(rawTime);
}
应该可以帮助您入门。 (SimpleDateFormat 的 Javadoc)
(请注意,您引用的日期看起来像,但不是 ISO 8601)。
我正在使用 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"
}
}
我的根 class 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;
}
}
转换为 java 对象后,callBackTime
值设置为当前时间,而其他字段具有正确的值。
我是 json 的新手,你能帮我找出我做错的地方吗?
假设您使用的 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 具有良好的声誉。否则,如果您显示的日期是预期的,则类似于:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void setCallBackTime(String rawTime) {
this.callBackTime = df.parse(rawTime);
}
应该可以帮助您入门。 (SimpleDateFormat 的 Javadoc)
(请注意,您引用的日期看起来像,但不是 ISO 8601)。