早上好家伙!
我有一个JSON字符串,看起来像:
{
"StatusCode":0,
"Message":null,
"ExecutionTime":0,
"ResponseData":[
{"Name":"name1","SiteId":"1234","Type":"Type1","X":"1234567","Y":"123456"},
{"Name":"Name2","SiteId":"2134","Type":"Type2","X":"1234567","Y":"1234567"},
{"Name":"Name3","SiteId":"3241","Type":"Type3","X":"1234567","Y":"1234567"},
{"Name":"Name4","SiteId":"4123","Type":"Type4","X":"123456","Y":"123456"}
]
}
我想创建一个对象,可以在其中检索
X
和Y
值。我一直在尝试使用Jackson来序列化JSON字符串,但没有成功。我创建了两个额外的类供Jackson使用。顶层的一类
StatusCode
,Message
,ExecutionTime
和ResponseData
看起来像public class PL {
private Long statusCode;
private String executionTime;
private String message;
private ResponseData responseData;
public PL(){
}
public void setStatusCode(Long statusCode){
this.statusCode = statusCode;
}
public Long getStatusCode(){
return this.statusCode;
}
public void setExecutionTime(String executionTime){
this.executionTime = executionTime;
}
public String getExecutionTime(){
return this.executionTime;
}
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return this.message;
}
public void setResponseData(ResponseData responseData){
this.responseData = responseData;
}
public ResponseData getResponseData(){
return this.responseData;
}
}
其中
ReponseData
作为对象返回,然后我有另一个类用于序列化ResponseData
,如下所示public class ResponseData {
private String name;
private String siteId;
private String type;
private String x;
private String y;
public ResponseData(){
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setSiteId(String siteId){
this.siteId = siteId;
}
public String getSiteId(){
return this.siteId;
}
public void setType(String type){
this.type = type;
}
public String setType(){
return this.type;
}
public void setX(String x){
this.x = x;
}
public String getX(){
return this.x;
}
public void setY(String y){
this.y = y;
}
public String getY(){
return this.y;
}
}
然后我用创建一个
ObjectMapper
private final static ObjectMapper mapper = new ObjectMapper();
并尝试这样读取值
ResponseData e = mapper.readValue(result.toString(), ResponseData.class);
并以例外结束
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“StatusCode”(类MyClass.ResponseData),未标记为可忽略(5个已知属性:“x”,“y”,“siteId”,“name”,“类型”])
好像它无法解析第一个条目
StatusMessage
。即使我删除第二个类并仅尝试解析前四个条目,在这些条目中我将ResponseData
作为String
返回,我仍然会遇到相同的异常。 最佳答案
首先,在PL
中,您应该拥有List<ResponseData>
,而不是简单的ResponseData
属性。如您所见,在JSON中,ResponseData
是一个"ResponseData":[...]
数组,因此它将被反序列化为List
。列表中的每个元素都是您定义的ResponseData
对象。
然后遇到一个大小写问题,您的JSON中有大写字母,而类属性中没有。您可以使用@JsonProperty
(See API)注释来克服此问题,方法是:
class PL {
@JsonProperty("StatusCode")
private Long statusCode;
@JsonProperty("ExecutionTime")
private String executionTime;
@JsonProperty("Message")
private String message;
@JsonProperty("ResponseData")
private List<ResponseData> responseDatas;
public PL(){
}
// getters/Setters
}
class ResponseData {
@JsonProperty("Name")
private String name;
@JsonProperty("SiteId")
private String siteId;
@JsonProperty("Type")
private String type;
@JsonProperty("X")
private String x;
@JsonProperty("Y")
private String y;
public ResponseData(){
}
// getters/Setters
}
然后将您的JSON作为
PL
对象读取,如下所示:ObjectMapper mapper = new ObjectMapper();
PL pl = mapper.readValue(json, PL.class);
for(ResponseData rd : pl.getResponseDatas()) {
System.out.println(rd.getX());
System.out.println(rd.getY());
}
输出:
1234567
123456
1234567
1234567
1234567
1234567
123456
123456
关于java - 使用Jackson在Java中无法序列化为JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50386188/