作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试反序列化我的 Xml 文件并将其映射到我的类,如果我没有将列表附加到我的类,它工作正常。
我已经使用 XMLElementWrapper 添加了静态类 jsonIgnoreProperties,但它仍然不起作用!
这是我调用的方法。
File file = new File(test.xml);
XmlMapper xmlMapper = new XmlMapper();
String xml = inputStreamToString(new FileInputStream(file));
Test test = xmlMapper.readValue(xml, Test.class);
这是我的类(class)。
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
import com.fasterxml.jackson.xml.annotate.JacksonXmlElementWrapper;
import com.fasterxml.jackson.xml.annotate.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "web-request-form")
public class Test {
@JsonProperty("attachments")
public Attachments attachments;
public Test(Attachments attachments) {
super();
this.attachments = attachments;
}
public Test() {
super();
}
public Attachments getAttachments() {
return attachments;
}
public void setAttachments(Attachments attachments) {
this.attachments = attachments;
}
public static class Attachments {
@JacksonXmlElementWrapper(localName = "attachment")
public List<Attachment> attachment;
public Attachments() {
super();
}
public Attachments(List<Attachment> attachment) {
super();
this.attachment = attachment;
}
public List<Attachment> getAttachment() {
return attachment;
}
public void setAttachment(List<Attachment> attachment) {
this.attachment = attachment;
}
}
public static class Attachment {
@JsonProperty("filename")
public String fileName;
@JsonProperty("desc")
public String desc;
@JsonProperty("size")
public String size;
public Attachment() {
super();
}
public Attachment(String fileName, String desc, String size) {
super();
this.fileName = fileName;
this.desc = desc;
this.size = size;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
}
这是我的 xml 文件。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--This file was generated from an ASPX file-->
<web-request-form xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="WebRequestForm.elms.xmlbeans.novacitynets.com">
<attachments>
<attachment>
<filename>test</filename>
<desc />
<size>2089.801</size>
</attachment>
</attachments>
</web-request-form>
这是我收到的错误。
Can not instantiate value of type [simple type, class
package.Test$Attachment] from JSON String; no single-String
constructor/factory method (through reference chain:
package.Test["attachments"]->package.Attachments["attachment"])
最佳答案
我不确定你的 Jackson 库版本,但如果你使用的是 2.1 或更高版本,你可以试试下面的代码吗?此外,混合使用 codehaus
和 fasterxml
似乎不太好。
尝试以下操作:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.util.List;
@JacksonXmlRootElement(localName = "web-request-form")
public class Test {
@JsonProperty("attachments")
public Attachments attachments;
public static class Attachments {
// @JacksonXmlElementWrapper(localName = "attachment")
@JacksonXmlElementWrapper(useWrapping = false, localName = "attachment")
public List<Attachment> attachment;
// ...
}
public static class Attachment {
@JsonProperty("filename")
public String fileName;
@JsonProperty("desc")
public String desc;
@JsonProperty("size")
public String size;
// ...
}
}
如果您已将 useWrapping
选项应用于 JacksonXmlElementWrapper
注释,则不会发生任何变化。调用方法如下:
File file = new File("test.xml");
// If you use the useWrapping option globally
// JacksonXmlModule module = new JacksonXmlModule();
// module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);
String xml = inputStreamToString(new FileInputStream(file));
Test test = xmlMapper.readValue(xml, Test.class);
关于java - 使用 Jackson 将 Xml 反序列化为列表失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56087719/
出于好奇,我尝试了一些原型(prototype)制作,但似乎只允许在第一个位置使用子例程的原型(prototype) &。 当我写作时 sub test (&$$) { do_somethin
我需要开发一个类似于 Android Play 商店应用程序或类似 this app 的应用程序.我阅读了很多教程,发现几乎每个教程都有与 this one 类似的例子。 . 我已经开始使用我的应用程
考虑一个表示“事件之间的时间”的列: (5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2) 我想将这些分组到 30 个桶中,但桶会重置。期望的结果: (0, 1,
我是一名优秀的程序员,十分优秀!