gpt4 book ai didi

java - 如何在 JAXB/JSON 中命名复杂对象?

转载 作者:行者123 更新时间:2023-12-02 04:26:32 24 4
gpt4 key购买 nike

@XmlRootElement(name = "test")
public class MyDTO {
@XmlElement(name = "test2)
private MyObject meta;
}

结果:

{meta:{...}}

问题:

  1. 我想要某种名为“test”的“外部”标签
  2. 为什么元的 @XmlElement(name"属性不起作用?

最佳答案

我的第一篇文章!

事实上,您可以使用 @XmlRootElement 命名您的“外部”标记。如果您需要另一个外部标签,我不知道如何实现这一点。

您的第二个问题可能是因为您放置 @XmlElement 的位置。我把它放在我的 getter 方法上,它对我来说工作得很好。

对于 JSON 输出,我使用 jersey-json-1.18。以下内容也适用于您可以定义的其他复杂类型,而不是“字符串元”。

这是我能够产生的输出:

作为 JSON

{"myId":"id1","myMeta":"text1"}

作为 XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mytupel>
<myId>id1</myId>
<myMeta>text1</myMeta>
</mytupel>

这是我的对象:

@XmlRootElement(name = "mytupel")
public class Tupel {
// @XmlElement(name = ) does not work here - defined it on the getter method
private String id;

// @XmlElement(name = ) does not work here - defined it on the getter method
private String meta;

/**
* Needed for JAXB
*/
public Tupel() {
}

/**
* For Test purpose...
*/
public Tupel(String id, String text) {
super();
this.id = id;
this.meta = text;
}

@XmlElement(name = "myId")
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@XmlElement(name = "myMeta")
public String getMeta() {
return meta;
}

public void setMeta(String meta) {
this.meta = meta;
}

/**
* For Test purpose...
*/
@Override
public String toString() {
return id + ": " + meta;
}
}

这是我的小类,用于生成输出 XML 文件...

public class Main {

private static final String TUPEL_1_XML = "./tupel1.xml";
private static final String TUPEL_2_XML = "./tupel2.xml";

public static void main(String[] args) throws JAXBException, FileNotFoundException {
// init JAXB context/Marhsaller stuff
JAXBContext context = JAXBContext.newInstance(Tupel.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Unmarshaller unmarshaller = context.createUnmarshaller();

// create some Datatypes
Tupel data1 = new Tupel("id1", "text1");
Tupel data2 = new Tupel("id2", "42");

// produce output
marshaller.marshal(data1, new File(TUPEL_1_XML));
marshaller.marshal(data2, new File(TUPEL_2_XML));

// read from produced output
Tupel data1FromXml = (Tupel) unmarshaller.unmarshal(new FileReader(TUPEL_1_XML));
Tupel data2FromXml = (Tupel) unmarshaller.unmarshal(new FileReader(TUPEL_2_XML));

System.out.println(data1FromXml.toString());
System.out.println(data2FromXml.toString());

System.out.println(marshalToJson(data1FromXml));
System.out.println(marshalToJson(data2FromXml));
}

public static String marshalToJson(Object o) throws JAXBException {
StringWriter writer = new StringWriter();
JAXBContext context = JSONJAXBContext.newInstance(o.getClass());

Marshaller m = context.createMarshaller();
JSONMarshaller marshaller = JSONJAXBContext.getJSONMarshaller(m, context);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshallToJSON(o, writer);
return writer.toString();
}

}

希望这能回答您的问题!干杯最大

关于java - 如何在 JAXB/JSON 中命名复杂对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32074718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com