猿问

将 JSON 解析为结果对象时无法反序列化对象的实例

我想将具有配置属性的 XML 文件解析为 JSON,然后将此 JSON 转换为最终结果对象。


我的班级看起来像:


@SpringBootApplication

public class AdvancedApplication {


  public static void main(String[] args) {


    SpringApplication.run(AdvancedApplication.class, args);


    XmlMapper xmlMapper = new XmlMapper();

    try {

      List XMLEntries = xmlMapper

          .readValue(new ClassPathResource("configuration.xml")

              .getFile(), List.class);


      ObjectMapper mapper = new ObjectMapper();

      String jsonConfig = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(XMLEntries);


      JsonNode parent = new ObjectMapper().readTree(jsonConfig);

      String content = parent.path("serverport").asText();


      System.out.println(content);


      System.out.println(jsonConfig);

    } catch (IOException e) {

      e.printStackTrace();

    }


  }


}


在第一种情况下:


List XMLEntries = xmlMapper

          .readValue(new ClassPathResource("configuration.xml")

              .getFile(), List.class);

上面的方法将 JSON 包装在列表中,结果如下:


[ {

  "serverport" : "9966"

}, {

  "clientport" : "9999",

  "serverHost" : "localhost"

} ]

但在这种情况下,我无法使用以下行读取值:


String content = parent.path("serverport").asText();

因为内容是空的。


最后,我决定以这种特殊方式将 JSON 转换为结果对象 Config:


Config configObject = mapper.readValue(jsonConfig, Config.class);

但不幸的是,我收到了一个异常,例如:


com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.javase.advanced.config.Config` out of START_ARRAY token

 at [Source: (String)"[ {

  "serverport" : "9966"

}, {

  "clientport" : "9999",

  "serverHost" : "localhost"

} ]"; line: 1, column: 1]

我的configuration.xml 文件如下所示:


<?xml version="1.0" encoding="UTF-8"?>

<config>

  <server serverport="9966"/>

  <client clientport="9999">

    <serverHost>localhost</serverHost>

  </client>

</config>

以及配置类如下:


@NoArgsConstructor

@Getter

@AllArgsConstructor

@ToString

public class Config {


  private Server server;

  private Client client;

}

我想要实现的是将configuration.xml 文件解析为JSON 并将其转换为Config 对象以创建配置类以供进一步使用。


缥缈止盈
浏览 159回答 2
2回答

慕雪6442864

这应该有效。您可以使用 JAXB 进行解组。请阅读有关 JAXB 的信息。还要注意如何使用XmlElement和。XmlAttributeString xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<config>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; <server serverport=\"9966\"/>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; <client clientport=\"9999\">\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; <serverHost>localhost</serverHost>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; </client>\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</config>";JAXBContext jaxbContext;&nbsp;try&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; jaxbContext = JAXBContext.newInstance(Config.class);&nbsp; &nbsp; &nbsp; Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();&nbsp; &nbsp; &nbsp; Config config = (Config) jaxbUnmarshaller.unmarshal(new StringReader(xmlString));&nbsp; &nbsp; &nbsp; System.out.println(config);&nbsp; &nbsp;}catch (JAXBException e){&nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; }配置类将是这样的@XmlRootElement(name = "config")@XmlAccessorType(XmlAccessType.PROPERTY)public class Config {&nbsp; &nbsp; public Server server;&nbsp; &nbsp; public Client client;&nbsp; &nbsp; public Config() {&nbsp; &nbsp; }&nbsp; &nbsp; public Server getServer() {&nbsp; &nbsp; &nbsp; &nbsp; return server;&nbsp; &nbsp; }&nbsp; &nbsp; public void setServer(Server server) {&nbsp; &nbsp; &nbsp; &nbsp; this.server = server;&nbsp; &nbsp; }&nbsp; &nbsp; public Client getClient() {&nbsp; &nbsp; &nbsp; &nbsp; return client;&nbsp; &nbsp; }&nbsp; &nbsp; public void setClient(Client client) {&nbsp; &nbsp; &nbsp; &nbsp; this.client = client;&nbsp; &nbsp; }}服务器类public class Server {&nbsp; &nbsp; @XmlAttribute(name = "serverport")&nbsp; &nbsp; public String serverPort;}客户类public class Client {&nbsp; &nbsp; @XmlAttribute(name = "clientport")&nbsp; &nbsp; public String clientPort;&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public String serverHost;}

饮歌长啸

该解决方案pvpkiran解决了一个问题,但我无法忍受 Jackson 无法将 XML 解析为单个对象的事实。调查带来了理想的效果,最终发现 我的 pom.XML 中有两个类似的依赖项,如jackson-databind和。jackson-xml-databind原来他们之间有矛盾。注释掉后jackson-xml-databind一切正常。现在我的课程看起来像:客户端类@Getter@NoArgsConstructor@Setterpublic class Client {&nbsp; @JacksonXmlProperty(localName = "clientport")&nbsp; private String clientPort;&nbsp; @JacksonXmlProperty(localName = "serverHost")&nbsp; private String serverHost;}服务器类@AllArgsConstructor@Getter@NoArgsConstructor@Setterpublic class Server {&nbsp; @JacksonXmlProperty(localName = "serverport")&nbsp; private String serverPort;}配置类@AllArgsConstructor@NoArgsConstructor@Getter@Setter@ToString@JacksonXmlRootElement(localName = "config")public class Config {&nbsp; private Server server;&nbsp; private Client client;}再次感谢您的承诺。
随时随地看视频慕课网APP

相关分类

Java
我要回答