使用 Jackson Java 反序列化具有没有值的属性的 XML 元素
Deserialize XML element with attributes with no value using Jackson Java
我正在尝试反序列化以下内容 XML 但我无法反序列化参数参数部分。
<video src="https://google.com/sample.mp4">
<param>s</param>
<param>Y</param>
<param>Z</param>
</video>
我的模型
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.util.ArrayList;
import java.util.List;
public class Video {
@JacksonXmlProperty(isAttribute = true)
private String src;
@JacksonXmlElementWrapper(localName = "param", useWrapping = false)
private List<String> param = new ArrayList<>();
public String getSrc() {
return src;
}
public List<String> getParam() {
return param;
}
public void setParam(List<String> param) {
this.param = param;
}
}
输出
{
"src": "https://google.com/sample.mp4",
"param": [
"Z"
]
}
我希望 param 的值类似于
{
"src": "https://google.com/sample.mp4",
"param": [
"s",
"Y",
"Z"
]
}
Java代码
ObjectMapper mapper = new ObjectMapper(new XmlFactory());
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Video video = mapper.readValue(s, Video.class);
System.out.println(new ObjectMapper(new JsonFactory()).writeValueAsString(video));
谁能帮我让它工作。谢谢。
我使用了以下代码并且对我有用,
XmlMapper mapper = new XmlMapper();
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Video video = mapper.readValue(s, Video.class);
System.out.println(new ObjectMapper(new JsonFactory()).writeValueAsString(video));
XmlMapper
来自包 com.fasterxml.jackson.dataformat.xml.XmlMapper
希望对你有所帮助。
您需要使用:
@JacksonXmlProperty(localName = "param")
@JacksonXmlElementWrapper(useWrapping = false)
private List<String> param = new ArrayList<>();
并删除 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
,因为它只会掩盖问题。
这段代码对我有用:
XmlMapper mapper = new XmlMapper();
Video video = mapper.readValue(s, Video.class);
System.out.println(new ObjectMapper(new JsonFactory()).writeValueAsString(video));
输出:
{"src":"https://google.com/sample.mp4","param":["s","Y","Z"]}
我正在尝试反序列化以下内容 XML 但我无法反序列化参数参数部分。
<video src="https://google.com/sample.mp4">
<param>s</param>
<param>Y</param>
<param>Z</param>
</video>
我的模型
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.util.ArrayList;
import java.util.List;
public class Video {
@JacksonXmlProperty(isAttribute = true)
private String src;
@JacksonXmlElementWrapper(localName = "param", useWrapping = false)
private List<String> param = new ArrayList<>();
public String getSrc() {
return src;
}
public List<String> getParam() {
return param;
}
public void setParam(List<String> param) {
this.param = param;
}
}
输出
{
"src": "https://google.com/sample.mp4",
"param": [
"Z"
]
}
我希望 param 的值类似于
{
"src": "https://google.com/sample.mp4",
"param": [
"s",
"Y",
"Z"
]
}
Java代码
ObjectMapper mapper = new ObjectMapper(new XmlFactory());
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Video video = mapper.readValue(s, Video.class);
System.out.println(new ObjectMapper(new JsonFactory()).writeValueAsString(video));
谁能帮我让它工作。谢谢。
我使用了以下代码并且对我有用,
XmlMapper mapper = new XmlMapper();
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Video video = mapper.readValue(s, Video.class);
System.out.println(new ObjectMapper(new JsonFactory()).writeValueAsString(video));
XmlMapper
来自包 com.fasterxml.jackson.dataformat.xml.XmlMapper
希望对你有所帮助。
您需要使用:
@JacksonXmlProperty(localName = "param")
@JacksonXmlElementWrapper(useWrapping = false)
private List<String> param = new ArrayList<>();
并删除 mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
,因为它只会掩盖问题。
这段代码对我有用:
XmlMapper mapper = new XmlMapper();
Video video = mapper.readValue(s, Video.class);
System.out.println(new ObjectMapper(new JsonFactory()).writeValueAsString(video));
输出: {"src":"https://google.com/sample.mp4","param":["s","Y","Z"]}