杰克逊:对父元素数组进行JSON解析:根名称''与预期不符('列表')

时间:2014-05-27 07:22:42

标签: json jackson

编写以下代码后,我收到以下异常。

引起:com.fasterxml.jackson.databind.JsonMappingException:根名称'提交'类型[集合类型;与预期('列表')不匹配; class java.util.List,包含[simple type,class MasMonthlyReportDetail]]

JSON对象

{
     "Filing":
    [
           {
                "periodInfo":
                    {
                         "date": "06-05-2013",
                 "year": "2015",
                 "Month": "January"

                    },
            "employerInfo":
                {
                 "name": "Y",
                 "place": "Y",
                 "country": "N",

                    },
                "employeeInfo":
                    [
                         {
                              "name": "785-23-0370",                  
                  "dob": "05/25/1952",

                         }
                    ],
                "messages": 
                [
                     {
                  "defaultMessage" : "xx",
                  "messageType" : "yy",
                  "messageCode" : "102"
                 }
                ]
          },

               {
                "periodInfo":
                    {
                         "date": "06-05-2013",
                 "year": "2015",
                 "Month": "January"

                    },
            "employerInfo":
                {
                 "name": "Y",
                 "place": "Y",
                 "country": "N",

                    },
                "employeeInfo":
                    [
                         {
                              "name": "785-23-0370",                  
                  "dob": "05/25/1952",

                         }
                    ],
                "messages": 
                [
                     {
                  "defaultMessage" : "xx",
                  "messageType" : "yy",
                  "messageCode" : "102"
                 }
                ]
          }
    ]
}

主要类

ObjectMapper objectMapper = new ObjectMapper();        

objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

List<MasMonthlyReportDetail> lcd =  objectMapper.readValue(new File(filePath),new TypeReference<List<MasMonthlyReportDetail>>(){});

MasMonthlyReportDetail.java

@JsonRootName(value="Filing")

public class MasMonthlyReportDetail {

    private PeriodInfo periodInfo;
    private EmployerInfo employerInfo;
    List<EmployeeInfo> employeeInfo;
    List<Messages> messages;

    public PeriodInfo getPeriodInfo() {
        return periodInfo;
    }
    public void setPeriodInfo(PeriodInfo periodInfo) {
        this.periodInfo = periodInfo;
    }
    public EmployerInfo getEmployerInfo() {
        return employerInfo;
    }
    public void setEmployerInfo(EmployerInfo employerInfo) {
        this.employerInfo = employerInfo;
    }
    public List<EmployeeInfo> getEmployeeInfo() {
        return employeeInfo;
    }
    public void setEmployeeInfo(List<EmployeeInfo> employeeInfo) {
        this.employeeInfo = employeeInfo;
    }
    public List<Messages> getMessages() {
        return messages;
    }
    public void setMessages(List<Messages> messages) {
        this.messages = messages;
    }

}

2 个答案:

答案 0 :(得分:3)

我做了以下更改,代码对我有用。

主类:

InputStream inputStream = resource.getInputStream();

ObjectMapper objectMapper = new ObjectMapper();

MasMonthlyReportDetailHolder masMonthlyReportDetailHolder = objectMapper.readValue(inputStream, MasMonthlyReportDetailHolder.class);

List<MasMonthlyReportDetail> masMonthlyReportDetail = masMonthlyReportDetailHolder.getMasMonthlyReportDetail();

MasMonthlyReportDetailHolder类:

public class MasMonthlyReportDetailHolder {

    private List<MasMonthlyReportDetail> masMonthlyReportDetail;

    @JsonProperty("Filing")
    public List<MasMonthlyReportDetail> getMasMonthlyReportDetail() {
        return masMonthlyReportDetail;
    }

    public void setMasMonthlyReportDetail(
            List<MasMonthlyReportDetail> masMonthlyReportDetail) {
        this.masMonthlyReportDetail = masMonthlyReportDetail;
    }

}

添加@JsonProperty("Filing")是避免此问题的关键。如有任何其他程序,请告诉我。

答案 1 :(得分:-2)

你试过这个吗? 。jacksonObjectMapper.reader(MasMonthlyReportDetail.class).withRootName( “归档”)readValue(jsonAsString);