我已经定义了 JSON Pojo 类,如下所示,在下面的 MyMessage 类中,有时我将 kpMessage 类作为字符串接收,如何将接收到的字符串转换为对象。
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "kp.message", "kp.version" })
public class MyMessage {
@JsonProperty("kp.message")
private KpMessage kpMessage;
@JsonProperty("kp.version")
private String kpVersion;
@JsonProperty("kp.message")
public KpMessage getKpMessage() {
return KpMessage;
}
@JsonSetter
public void setKpMessage(KpMessage kpMessage) {
this.kpMessage = kpMessage;
}
@JsonProperty("kp.version")
public String getKpVersion() {
return kpVersion;
}
@JsonProperty("kp.version")
public void setKpVersion(String kpVersion) {
this.kpVersion = kpVersion;
}
}
KpMessage.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "domain", "request_id", "auth_token"})
public class KpMessage {
@JsonProperty("domain")
private Object domain;
@JsonProperty("request_id")
private String requestId;
@JsonProperty("auth_token")
private String authToken;
@JsonProperty("domain")
public Object getDomain() {
return domain;
}
@JsonProperty("domain")
public void setDomain(Object domain) {
this.domain = domain;
}
@JsonProperty("request_id")
public String getRequestId() {
return requestId;
}
@JsonProperty("request_id")
public void setRequestId(String requestId) {
this.requestId = requestId;
}
@JsonProperty("auth_token")
public String getAuthToken() {
return authToken;
}
@JsonProperty("auth_token")
public void setAuthToken(String contextAuthToken) {
this.authToken = authToken;
}
}
下面的示例输入按预期工作。
{
"kp.message":{
"domain":null,
"request_id":"req-11ef0ffa-0180-4040-a47e-9f78b23b49e9",
"auth_token":"gAAAA"
},
"kp.version":"2.0"
}
但是以下输入失败并出现错误no String-argument constructor/factory method to deserialize from String value
{
"kp.message": "{
\"domain\": null,
\"request_id\": \"req-11ef0ffa-0180-4040-a47e-9f78b23b49e9\",
\"auth_token\": \"gAAAA\",
}",
"kp.version": "2.0"
}
如何为上述输入添加反序列化器,以便如果收到 String 由于转义字符"
,则将 String 转换为KpMessage
Object