使用Jackson Java反序列化没有值的带有属性的XML元素

huangapple go评论71阅读模式
英文:

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.

&lt;video src=&quot;https://google.com/sample.mp4&quot;&gt;
	&lt;param&gt;s&lt;/param&gt;
	&lt;param&gt;Y&lt;/param&gt;
	&lt;param&gt;Z&lt;/param&gt;
&lt;/video&gt;

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 = &quot;param&quot;, useWrapping = false)
    private List&lt;String&gt; param = new ArrayList&lt;&gt;();

    public String getSrc() {
        return src;
    }

    public List&lt;String&gt; getParam() {
        return param;
    }

    public void setParam(List&lt;String&gt; param) {
        this.param = param;
    }
}

Output

{
	&quot;src&quot;: &quot;https://google.com/sample.mp4&quot;,
	&quot;param&quot;: [
		&quot;Z&quot;
	]
}

I am expecting the values of param to be something like

{
	&quot;src&quot;: &quot;https://google.com/sample.mp4&quot;,
	&quot;param&quot;: [
        &quot;s&quot;,
        &quot;Y&quot;,
		&quot;Z&quot;
	]
}

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 = &quot;param&quot;)
@JacksonXmlElementWrapper(useWrapping = false)
private List&lt;String&gt; param = new ArrayList&lt;&gt;();

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.

huangapple
  • 本文由 发表于 2020年4月10日 02:00:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61127365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定