这是我的 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;
}
这就是我的 pojo 将 JSON 解析为 XML 的样子
@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 的属性?请帮忙!提前非常感谢。
最佳答案
您不需要两个 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 {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
System.out.println(jsonToXml());
}
/**
* json to xml transformation
*/
public static String jsonToXml() throws JsonParseException, JsonMappingException, IOException{
String json = "{\r\n \"field1\": \"value1\",\r\n \"field2\": \"value2\",\r\n \"field3\": \"value3\",\r\n \"field4\": \"value4\",\r\n \"field5\": \"value5\"\r\n }";
return new XmlMapper().writeValueAsString(new ObjectMapper().readValue(json, Message.class));
}
}
class Message {
@JacksonXmlProperty(localName = "element1")
String element1;
@JacksonXmlProperty(localName = "element2")
String element2;
@JacksonXmlProperty(localName = "element3")
Elements elements;
@JsonCreator
public Message(@JsonProperty("field1") String element1, @JsonProperty("field2") String element2,
@JsonProperty("field3") String element3, @JsonProperty("field4") String element4, @JsonProperty("field5") String element5) {
super();
this.element1 = element1;
this.element2 = element2;
this.elements = new Elements(element3, element4, element5);
}
}
class Elements{
public Elements(String element3, String element4, String element5) {
super();
this.element3 = element3;
this.element4 = element4;
this.element5 = element5;
}
@JacksonXmlText
String element3;
@JacksonXmlProperty(localName = "element4", isAttribute = true)
String element4;
@JacksonXmlProperty(localName = "element5", isAttribute = true)
String element5;
}
输入:
{
"field1": "value1",
"field2": "value2",
"field3": "value3",
"field4": "value4",
"field5": "value5"
}
输出:
<Message>
<element1>value1</element1>
<element2>value2</element2>
<element3 element4="value4" element5="value5">value3</element3>
</Message>
关于java - 使用 JACKSON 从 JSON 输入向 XML 添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51508459/