首页 > 解决方案 > JSON 未命名集合到 Java (jackson ObjectMapper)

问题描述

我尝试寻找类似的问题,发现了这个Use Jackson to parse and unnamed array

但是,我有一个问题。我的 JSON 在第 N 级上有这个未命名的数组 - 非常简化的版本:

{
   "section":{
      "ID":"",
      "code":{
         "code":"",
         "codeSystem":"",
         "codeSystemName":""
      },
      "entry":[
         {
            "act":{
               "classCode":"",
               "entryRelationship":{
                  "name":""
               }
            },
            "id":""
         },
         {
            "act":{
               "classCode":"",
               "entryRelationship":{
                  "name":""
               }
            },
            "id":""
         }
      ]
   }
}

还有比section更高的级别,但这应该是主要问题。我有这个“入口”键,它可以有多个“行为”和“id”(作为未命名对象的属性)。我已经在使用 jackson 库中的 ObjectMapper 和 @JsonProperty 工具,我想保持干净。该部分的一段代码(同样,非常简化):

public class ComponentSection {

    @JsonProperty("ID")
    private String id;
    @JsonProperty("code")
    private Code code;
    @JsonProperty("entry")
    private SectionEntry entry;
}

我能做些什么来映射这个 JSON 吗?

谢谢!

标签: javajson

解决方案


改变

@JsonProperty("entry")
private SectionEntry entry;

@JsonProperty("entry")
private List<SectionEntry> entry;

推荐阅读