gpt4 book ai didi

java - 数组上的 JAXB JSON 强制括号

转载 作者:搜寻专家 更新时间:2023-11-01 03:29:41 26 4
gpt4 key购买 nike


我试图在仅包含一个元素的列表上强制使用括号。

我想要这样的东西:
{"id":"0","industries":[{"id":"0","name":"Technologies"}],"name":"Google Inc."

但我得到:
{"id":"0","industries":{"id":"0","name":"Technologies"},"name":"Google Inc."

这是我的实体:

@Entity
@XmlRootElement
public class Company {
private int id;

private String name;
private String description;

@XMLElement(name="industries")
private List<Industry> industryList;

[...]

最后,我的 JAXB 上下文解析器:

public JAXBContextResolver() throws Exception {

MappedBuilder 构建器 = JSONConfiguration.mapped(); builder.arrays("行业"); builder.rootUnwrapping(true);

this.context = new JSONJAXBContext(builder.build(), Company.class);

最佳答案

感谢您的帮助,但我找到了答案。您实际上需要指定一个指定自然 JSON 配置的 JAXBContextResolver。您需要提供需要转换为 JSON 的每个容器的类型列表。在此示例中,您可以看到我指定了作为 Company 容器的 GetCompanyResponse。

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class[] types = { GetCompanyResponse.class };

public JAXBContextResolver() throws Exception {
this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), types);
}

public JAXBContext getContext(Class<?> objectType) {
for (Class clazz : types) {
if (clazz.equals(objectType)) {
return context;
}
}

return null;
}
}

关于java - 数组上的 JAXB JSON 强制括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3946102/

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