英文:
Deserialize JSON from a JSON array
问题
{
"Warnings": [
{
"Message": "账户代码 '48s9' 已被删除,因为它不匹配已识别的账户"
},
{
"Message": "账户代码 '48s9' 已被删除,因为它不匹配已识别的账户"
}
]
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class WarningsClass {
private String Message;
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
}
英文:
I am using Jackson and I am trying to deserialize a JSON response that looks like the following:
{
"Warnings": [{
"Message": "Account code '48s9' has been removed as it does not match a recognised account"
},
{
"Message": "Account code '48s9' has been removed as it does not match a recognised account"
}
]
}
am I correct in saying that the class would look as follows? Is there some way this can be done?
@JsonIgnoreProperties(ignoreUnknown = true)
public class WarningsClass {
private String Message;
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
}
答案1
得分: 1
// 成功找到解决方法:
// 导入 com.fasterxml.jackson.databind.ObjectMapper; // 版本 2.11.1
// 导入 com.fasterxml.jackson.annotation.JsonProperty; // 版本 2.11.1
/* ObjectMapper om = new ObjectMapper();
Root root = om.readValue(myJsonString), Root.class); */
public class Warning{
@JsonProperty("Message")
public String message;
}
public class WarningsClass{
@JsonProperty("Warnings")
public List<Warning> warnings;
}
WarningsClass messageResponse = mapper.readValue(data, WarningsClass.class);
类似这样的代码将起作用。
英文:
was able to figure it out:
// import com.fasterxml.jackson.databind.ObjectMapper; // version 2.11.1
// import com.fasterxml.jackson.annotation.JsonProperty; // version 2.11.1
/* ObjectMapper om = new ObjectMapper();
Root root = om.readValue(myJsonString), Root.class); */
public class Warning{
@JsonProperty("Message")
public String message;
}
public class WarningsClass{
@JsonProperty("Warnings")
public List<Warning> warnings;
}
WarningsClass messageResponse = mapper.readValue(data, WarningsClass.class);
something like this would work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论