XMl使用Jackson通过Java Pojo转换为JSON

时间:2019-06-06 22:12:34

标签: java json xml jackson

我正在尝试将xml转换为json。首先,我使用以下xml创建了Java类

testVar1

以下Java类如下:

<CompositeResponse>
   <CompositeIndividualResponse>
      <PersonIdentification>2222</PersonIdentification>
   </CompositeIndividualResponse>
</CompositeResponse>

但是我遇到了以下异常,即CompositeIndividualResponse无法识别。

public class Main {
    public CompositeResponse CompositeResponse;
    public CompositeResponse getCompositeResponse() {
        return CompositeResponse;
    }
    public void setCompositeResponse(CompositeResponse CompositeResponse) {
        this.CompositeResponse = CompositeResponse;
    }
}

public class CompositeResponse {
    private List<CompositeIndividualResponse> CompositeIndividualResponse;

public List<CompositeIndividualResponse> getCompositeIndividualResponse() {
    return CompositeIndividualResponse;
}
public void setCompositeIndividualResponse(List<CompositeIndividualResponse> CompositeIndividualResponse) {
    CompositeIndividualResponse = CompositeIndividualResponse;
}
}

public class CompositeIndividualResponse {

    private String Persondentification;

    public String getPersondentification() {
        return Persondentification;
    }
    public void setPersonIdentification (String PersonIdentification) {
      this.PersonIdentification = PersonIdentification; 
    }

}

I am using the following code for conversion: 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class XMLToJson {
    public static void main(String[] args) throws IOException {
        String content = new String(Files.readAllBytes(Paths.get("test.xml")));
        XmlMapper xmlMapper = new XmlMapper();
        Main poppy = xmlMapper.readValue(content, Main.class);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(poppy);
        System.out.println(json);
    }
}

我相信我的java pojo不适合xml数据。因此,如何定义pojo的集合来解决此问题,以便获得以下json:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "CompositeIndividualResponse" (class com.test.custom.copy.Main), not marked as ignorable (2 known properties: "CompositeResponse", "compositeResponse"])
 at [Source: (StringReader); line: 3, column: 32] (through reference chain: com.test.custom.copy.Main["CompositeIndividualResponse"])

3 个答案:

答案 0 :(得分:1)

像这样定义您的POJO,

public class CompositeResponse {
     private List<CompositeIndividualResponse> compositeIndividualResponse;

     public List<CompositeIndividualResponse> getCompositeIndividualResponse() {
          return compositeIndividualResponse;
     }

    public void setCompositeIndividualResponse(List<CompositeIndividualResponse> compositeIndividualResponse) {
         CompositeIndividualResponse = compositeIndividualResponse;
     }
}

public class CompositeIndividualResponse {
    private String personIdentification;

    public String getPersonIdentification() {
        return personIdentification;
    }
    public void setPersonIdentification (String personIdentification) {
      this.personIdentification= personIdentification; 
    }
}

然后按如下所示更新您的主程序,

public class XMLToJson {
    public static void main(String[] args) throws IOException {
        String content = new String(Files.readAllBytes(Paths.get("test.xml")));
        XmlMapper xmlMapper = new XmlMapper();
        CompositeResponse poppy = xmlMapper.readValue(content, CompositeResponse.class);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(poppy);
        System.out.println(json);
    }
}

答案 1 :(得分:0)

字段命名问题,默认情况下所有字段均以小写字母开头。 例如:compositeResponse

为避免此问题,请为每个字段添加注释@JsonProperty,如下所示:

    @JsonProperty("persondentification")
    private String Persondentification;

答案 2 :(得分:0)

解决方案是我不需要Main.java类。另外,我需要添加杰克逊注释来定义xml元素。工作代码如下。

CompositeResponse.java

    public class CompositeResponse {

        @JacksonXmlProperty(localName = "CompositeIndividualResponse")
        @JacksonXmlElementWrapper(localName = "CompositeIndividualResponse",useWrapping = false)
        private List<CompositeIndividualResponse> compositeIndividualResponse;

        public List<CompositeIndividualResponse> getCompositeIndividualResponse() {
             return compositeIndividualResponse;
        }

       public void setCompositeIndividualResponse(List<CompositeIndividualResponse> compositeIndividualResponse) {
           this.compositeIndividualResponse = compositeIndividualResponse;
        }

    }

CompositeIndividualResponse.java: 

    public class CompositeIndividualResponse {

            @JacksonXmlProperty(localName = "PersonIdentification")
            private String personIdentification;

            public String getPersonIdentification() {
                return personIdentification;
            }
            public void setPersonIdentification (String personIdentification) {
              this.personIdentification= personIdentification; 
            }
    }

XMLToJson.java

    public class XMLToJson {
        public static void main(String[] args) throws IOException {
            String content = new String(Files.readAllBytes(Paths.get("test.xml")));
            XmlMapper xmlMapper = new XmlMapper();
            CompositeResponse poppy = xmlMapper.readValue(content, CompositeResponse.class);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(poppy);
            System.out.println(json);
        }
    }