带有嵌套对象的 ObjectMapper
ObjectMapper with nested object
我有 JSON 数组响应,但不是映射和抛出错误,我如何处理 JSON 像这样
{
"total":10,
"count":10,
"start":0,
"items":[
{
"ID":"Hyd4VVGtCNpyLh7JEOL3X-b92yollu9nb3KbweWxNaI=@mailhog.example",
"From":{"Relays":null,"Mailbox":"emails","Domain":"gmail.com","Params":""},
"To":[{"Relays":null,"Mailbox":"test","Domain":"gmail.com","Params":""}],
"Content":{"Headers":{"Content-Type":["multipart/mixed; \tboundary=\"----=_Part_27_47303298.1622881481751\""],
"Date":["Sat, 5 Jun 2021 11:24:42 +0300 (EEST)"],
"From":["test@gmail.com"],
"MIME-Version":["1.0"],
我试过这样映射
public static class ResponseDto{
Integer total;
Integer count;
Integer start;
List<ItemDto> items;
//getters,setters
}
public static class ItemDto{
String id;
}
我得到一个错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ID" (class package.api.utils.ApiMailHogUtil$ItemDto), not marked as ignorable (one known property: "id"])
at [Source: (StringReader); line: 1, column: 50] (through reference chain: package.api.utils.ApiMailHogUtil$ResponseDto["items"]->java.util.ArrayList[0]->package.api
我做错了什么?
如评论所述,Jackson
未找到“ID”字段,因此抛出异常。您可以在 ItemDto
中的 id
字段顶部添加 @JsonProperty("ID")
:
public static class ItemDto{
@JsonProperty("ID")
String id;
// other fields
}
我有 JSON 数组响应,但不是映射和抛出错误,我如何处理 JSON 像这样
{
"total":10,
"count":10,
"start":0,
"items":[
{
"ID":"Hyd4VVGtCNpyLh7JEOL3X-b92yollu9nb3KbweWxNaI=@mailhog.example",
"From":{"Relays":null,"Mailbox":"emails","Domain":"gmail.com","Params":""},
"To":[{"Relays":null,"Mailbox":"test","Domain":"gmail.com","Params":""}],
"Content":{"Headers":{"Content-Type":["multipart/mixed; \tboundary=\"----=_Part_27_47303298.1622881481751\""],
"Date":["Sat, 5 Jun 2021 11:24:42 +0300 (EEST)"],
"From":["test@gmail.com"],
"MIME-Version":["1.0"],
我试过这样映射
public static class ResponseDto{
Integer total;
Integer count;
Integer start;
List<ItemDto> items;
//getters,setters
}
public static class ItemDto{
String id;
}
我得到一个错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ID" (class package.api.utils.ApiMailHogUtil$ItemDto), not marked as ignorable (one known property: "id"])
at [Source: (StringReader); line: 1, column: 50] (through reference chain: package.api.utils.ApiMailHogUtil$ResponseDto["items"]->java.util.ArrayList[0]->package.api
我做错了什么?
如评论所述,Jackson
未找到“ID”字段,因此抛出异常。您可以在 ItemDto
中的 id
字段顶部添加 @JsonProperty("ID")
:
public static class ItemDto{
@JsonProperty("ID")
String id;
// other fields
}