使用 JACKSON 从 JSON 输入向 XML 添加属性

这是我的 JSON


    {

      "field1": "value1",

      "field2": "value2",

      "field3": "value3",

      "field4": "value4",

      "field5": "value5"

    }

这是我想转换为的 XML:


<root>

    <element1>value1</element1>

    <element2>value2</element2>

    <element3 element4="value4" element5="value5">value3</element3>

</root>

所以基本上,我想将元素 4 和 5 作为元素 3 的属性。希望到目前为止我是有道理的。


这就是我解析 JSON 的 pojo 的样子


public class JSONMessage {


    Date timestamp;


    @JsonProperty("field1")

    @JacksonXmlProperty(localName = "element1")

    String element1;


    @JsonProperty("field2")

    @JacksonXmlProperty(localName = "element2")

    String element2;


    @JsonProperty("field3")

    @JacksonXmlProperty(localName = "element3")

    String element3;


    @JsonProperty("field4")

    @JacksonXmlProperty(localName = "element4")

    String element4;


    @JsonProperty("field5")

    @JacksonXmlProperty(localName = "element5")

    String element5;

}

这就是我将 JSON 解析为 XML 的 pojo 的样子


@JacksonXmlRootElement(localName = "linkFoundEvent")

public class XMLMessage {

    private Date element1;

    private String element1;

    private String element2;

    @JacksonXmlProperty(localName = "element3")

    private Element3 element3;


}

对于 Element3,我编写了这个类 -


public class Element3{

    @JacksonXmlText

    private String element3;

    @JacksonXmlProperty(localName = "element4", isAttribute = true)

    private String element4;

    @JacksonXmlProperty(localName = "element5", isAttribute = true)

    private String element5;

}

如何将 Element4 和 Element5 作为 Element4 的属性?请帮忙!非常感谢。


拉风的咖菲猫
浏览 392回答 1
1回答

扬帆大鱼

您不需要两个 POJO 类(一个用于 JSON,一个用于 XML)来实现从输入 json 到输出 xml 的转换(如果这就是您想要的),请查看以下完整的工作代码:import java.io.IOException;import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonProperty;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.dataformat.xml.XmlMapper;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;public class JsonXmlTransformation {&nbsp; &nbsp; public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(jsonToXml());&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* json to xml transformation&nbsp;&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static String jsonToXml() throws JsonParseException, JsonMappingException, IOException{&nbsp; &nbsp; &nbsp; &nbsp; String json = "{\r\n&nbsp; &nbsp; &nbsp; \"field1\": \"value1\",\r\n&nbsp; &nbsp; &nbsp; \"field2\": \"value2\",\r\n&nbsp; &nbsp; &nbsp; \"field3\": \"value3\",\r\n&nbsp; &nbsp; &nbsp; \"field4\": \"value4\",\r\n&nbsp; &nbsp; &nbsp; \"field5\": \"value5\"\r\n&nbsp; &nbsp; }";&nbsp; &nbsp; &nbsp; &nbsp; return new XmlMapper().writeValueAsString(new ObjectMapper().readValue(json, Message.class));&nbsp; &nbsp; }}class Message {&nbsp; &nbsp; @JacksonXmlProperty(localName = "element1")&nbsp; &nbsp; String element1;&nbsp; &nbsp; @JacksonXmlProperty(localName = "element2")&nbsp; &nbsp; String element2;&nbsp; &nbsp; @JacksonXmlProperty(localName = "element3")&nbsp; &nbsp; Elements elements;&nbsp; &nbsp; @JsonCreator&nbsp; &nbsp; public Message(@JsonProperty("field1") String element1, @JsonProperty("field2") String element2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty("field3") String element3, @JsonProperty("field4") String element4, @JsonProperty("field5") String element5) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.element1 = element1;&nbsp; &nbsp; &nbsp; &nbsp; this.element2 = element2;&nbsp; &nbsp; &nbsp; &nbsp; this.elements = new Elements(element3, element4, element5);&nbsp;&nbsp; &nbsp; }}class Elements{&nbsp; &nbsp; public Elements(String element3, String element4, String element5) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.element3 = element3;&nbsp; &nbsp; &nbsp; &nbsp; this.element4 = element4;&nbsp; &nbsp; &nbsp; &nbsp; this.element5 = element5;&nbsp; &nbsp; }&nbsp; &nbsp; @JacksonXmlText&nbsp; &nbsp; String element3;&nbsp; &nbsp; @JacksonXmlProperty(localName = "element4", isAttribute = true)&nbsp; &nbsp; String element4;&nbsp; &nbsp; @JacksonXmlProperty(localName = "element5", isAttribute = true)&nbsp; &nbsp; String element5;}输入:{&nbsp; &nbsp; &nbsp; "field1": "value1",&nbsp; &nbsp; &nbsp; "field2": "value2",&nbsp; &nbsp; &nbsp; "field3": "value3",&nbsp; &nbsp; &nbsp; "field4": "value4",&nbsp; &nbsp; &nbsp; "field5": "value5"&nbsp; &nbsp; }输出:<Message>&nbsp; &nbsp; <element1>value1</element1>&nbsp; &nbsp; <element2>value2</element2>&nbsp; &nbsp; <element3 element4="value4" element5="value5">value3</element3></Message>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java