简体   繁体   English

JSON 到 Java 对象 - 无法识别的字段,未标记为可忽略

[英]JSON to Java object - Unrecognized field, not marked as ignorable

I'm trying to transform the following JSON into a java Object.我正在尝试将以下 JSON 转换为 java 对象。

{
  "Data":[
    {
      "AccountId":"2009852923",
      "Currency":"EUR",
      "Nickname":"SA 01",
      "Account":{
        "SchemeName":"BBAN",
        "Name":"SA 01",
        "Identification":"2009852923"
      },
      "Servicer":{
        "SchemeName":"BICFI",
        "Identification":"FNBSZAJJ"
      }
    },
    {
      "AccountId":"1028232942",
      "Currency":"EUR",
      "Nickname":"FNBCREDIT",
      "Account":{
        "SchemeName":"BBAN",
        "Name":"FNBCREDIT",
        "Identification":"1028232942"
      },
      "Servicer":{
        "SchemeName":"BICFI",
        "Identification":"FNBSZAJJ"
      }
    }
  ],
  "Links":{
    "self":"http://localhost:3000/api/open-banking/accounts/1009427721/transactions"
  },
  "Meta":{
    "total-pages":1
  }
}

Using the following DTO (for brevity, the referenced classes haven't been posted).使用以下 DTO(为简洁起见,尚未发布引用的类)。

public class TransactionDTO {
    private Data[] data;
    private Links links;
    private Meta meta;
    public Data[] getData () {  return data; }
    public void setData (Data[] data) { this.data = data; }
    public Links getLinks () { return links; }
    public void setLinks (Links links) { this.links = links; }
    public Meta getMeta () { return meta; }
    public void setMeta (Meta meta) { this.meta = meta; }
}

The code to transform the DTO to a Java object being:将 DTO 转换为 Java 对象的代码是:

private TransactionDTO marshall(String accountTransactionsJSON) {
    ObjectMapper objectMapper = new ObjectMapper();
    TransactionDTO transactionDTO = null;
    try {
        transactionDTO = objectMapper.readValue(accountTransactionsJSON, TransactionDTO.class);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return transactionDTO;
}

I'm getting this error:我收到此错误:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Data" (class xxx.dto.TransactionDTO), not marked as ignorable (3 known properties: "links", "data", "meta"])
 at [Source: java.io.StringReader@48f43b70; line: 2, column: 11] (through reference chain: xxx.dto.TransactionDTO["Data"])

I tried different approach to solve this issue such as:我尝试了不同的方法来解决这个问题,例如:

objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

As well as:也:

@JsonRootName(value = "data")

But I either get the same problem, or no problems, but the TransactionDTO containing null values only.但我要么遇到同样的问题,要么没有问题,但TransactionDTO仅包含null值。

I guess the problem is the Data field, but I don't know how to fix this problem (the solutions here don't work for me neither).我猜问题是Data字段,但我不知道如何解决这个问题(这里的解决方案对我也不起作用)。

Questions问题

  1. Any idea how to fix this problem ?知道如何解决这个问题吗?
  2. Should the accessors case reflect the case in the JSON ?访问器案例是否应该反映 JSON 中的案例?

Jackson is case sensitive by default. Jackson 默认区分大小写。 Try this:尝试这个:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

I solved a similar problem using this aproach我使用这种方法解决了类似的问题

ObjectMapper objectMapper = new ObjectMapper();

objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

The problem is that your JSON property names (eg "Data" ) don't match your Java property names (eg data ).问题是您的 JSON 属性名称(例如"Data" )与您的 Java 属性名称(例如data )不匹配。 Besides @psmagin's answer there are two alternative options to fix it:除了@psmagin 的回答之外,还有两个替代选项可以修复它:

  1. Keep your Java code unchanged.保持 Java 代码不变。 And in the JSON contents change all keys (the strings left from the : ) from first-uppercase to first-lowercase:并在 JSON 内容中将所有键(从:留下的字符串)从第一个大写更改为第一个小写:

     { "data":[ { "accountId":"2009852923", "currency":"EUR", "nickname":"SA 01", "account":{ "schemeName":"BBAN", "name":"SA 01", "identification":"2009852923" }, .... }
  2. Keep the JSON contents unchanged.保持 JSON 内容不变。 And in your Java-code use @JsonProperty annotations to tell Jackson the corresponding JSON property-names of your Java properties:在您的 Java 代码中,使用@JsonProperty批注告诉 Jackson 您的 Java 属性的相应 JSON 属性名称:

     public class TransactionDTO { private @JsonProperty("Data") Data[] data; private @JsonProperty("Links") Links links; private @JsonProperty("Meta") Meta meta; public Data[] getData () { return data; } public void setData (Data[] data) { this.data = data; } public Links getLinks () { return links; } public void setLinks (Links links) { this.links = links; } public Meta getMeta () { return meta; } public void setMeta (Meta meta) { this.meta = meta; } }

    and in the same manner in your other Java classes ( Links , Meta , Data , ...)并以相同的方式在您的其他 Java 类( LinksMetaData ,...)

I would prefer the first option, because property names with first-lowercase are the established best practice in JSON and Java.我更喜欢第一个选项,因为第一个小写的属性名称是 JSON 和 Java 中既定的最佳实践。

I got this error as I did not intend to map all the JSON fields to my POJO, but only a few.我收到此错误,因为我不打算将所有 JSON 字段映射到我的 POJO,但只有少数。 Consequently, it was asking me to mark them ignore.因此,它要求我将它们标记为忽略。 Following sample presents the idea:以下示例展示了这个想法:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Book {

@JsonProperty("kind")
private String kind;

@JsonProperty("id")
private String id;

@JsonProperty("volumeInfo")
private BookInfo bookInfo;

@Override
public String toString() {
    return "ClassPojo [kind = " + kind + ", id = " + id + ", bookInfo = " + bookInfo +"]";
}

On the other hand, my Json response carried out 10+ fields.另一方面,我的 Json 响应执行了 10 多个字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未被识别的字段,未标记为可忽略 - unrecognized field, not marked as ignorable Jackson Json 反序列化:无法识别的字段“...”,未标记为可忽略 - Jackson Json Deserialisation: Unrecognized field “…” , not marked as ignorable Jackson 与 JSON:无法识别的字段,未标记为可忽略 - Jackson with JSON: Unrecognized field, not marked as ignorable JSON:无法识别的字段“值”( <objectClass> ),没有标记为可忽略的 - JSON : Unrecognized field “value” (<objectClass>), not marked as ignorable jackson java无法识别的字段未标记为可忽略 - jackson java Unrecognized field not marked as ignorable 无法识别字段-未标记为可忽略错误-JSON-Java对象 - unable to recognize field - not marked as ignorable error - JSON - Java object 无法识别的字段类未标记为可忽略 - Unrecognized field class not marked as ignorable 将 JSON 映射到 POJO 进行处理 - 无法识别的字段未标记为可忽略 - Map JSON to POJO For Processing - Unrecognized field not marked as ignorable 解析json字符串时,无法识别的字段(未标记为可忽略) - Unrecognized field , not marked as ignorable ,when parsing json string Spring Jackson-无法识别的字段“ response”在以下位置未标记为可忽略 - Spring Jackson - Unrecognized field \“response\” not marked as ignorable at
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM