英文:
Deserialize XML element with attributes with no value using Jackson Java
问题
我正在尝试反序列化以下 XML,并且无法对参数 param 部分进行反序列化。
<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));
有人可以帮我让它正常工作吗?谢谢。
英文:
I am trying to deserialize the following XML and I couldn't get the parameter param section deserialized.
<video src="https://google.com/sample.mp4">
	<param>s</param>
	<param>Y</param>
	<param>Z</param>
</video>
My model
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;
    }
}
Output
{
	"src": "https://google.com/sample.mp4",
	"param": [
		"Z"
	]
}
I am expecting the values of param to be something like
{
	"src": "https://google.com/sample.mp4",
	"param": [
        "s",
        "Y",
		"Z"
	]
}
Java code
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));
Can someone help me getting it working. Thank you.
答案1
得分: 2
你需要使用:
@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"]}
英文:
You need to use:
@JacksonXmlProperty(localName = "param")
@JacksonXmlElementWrapper(useWrapping = false)
private List<String> param = new ArrayList<>();
and delete mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); as it only masks the issue.
This code worked for me:
XmlMapper mapper = new XmlMapper();
Video video = mapper.readValue(s, Video.class);
System.out.println(new ObjectMapper(new JsonFactory()).writeValueAsString(video));
Outputs:
{"src":"https://google.com/sample.mp4","param":["s","Y","Z"]}
答案2
得分: 1
我使用了以下代码,而且它对我起作用了,
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。
希望它对你有帮助。
英文:
I used the following code and it worked for me,
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 is frorm package com.fasterxml.jackson.dataformat.xml.XmlMapper
Hope it helped you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论