JAXB JSON 强制数组上的括号
我试图在仅包含一个元素的列表上强制加上括号。
我想要这样的东西:
{"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 builder = JSONConfiguration.mapped(); builder.arrays(“行业”); builder.rootUnwrapping(true);
this.context = new JSONJAXBContext(builder.build(), Company.class); }
I'm trying to force brackets on lists that contain only one element.
I want something like this:
{"id":"0","industries":[{"id":"0","name":"Technologies"}],"name":"Google Inc."}
But I get:
{"id":"0","industries":{"id":"0","name":"Technologies"},"name":"Google Inc."}
Here is my Entity:
@Entity
@XmlRootElement
public class Company {
private int id;
private String name;
private String description;
@XMLElement(name="industries")
private List<Industry> industryList;
[...]
And finally, my JAXB Context Resolver:
public JAXBContextResolver() throws Exception {
MappedBuilder builder = JSONConfiguration.mapped();
builder.arrays("industries");
builder.rootUnwrapping(true);
this.context = new JSONJAXBContext(builder.build(), Company.class);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢您的帮助,但我找到了答案。您实际上需要指定一个 JAXBContextResolver 来指定自然的 JSON 配置。您需要提供需要转换为 JSON 的每个容器的类型列表。在此示例中,您可以看到我指定了作为 Company 容器的 GetCompanyResponse。
thanks for your help, but I found the answer. You actually need to specify a JAXBContextResolver which specify natural JSON configuration. You need to provide a type list of every container that need to be transform to JSON. In this example, you can see that I specified the GetCompanyResponse that is a container of Company.
注意:我是EclipseLink JAXB (MOXy) 的领导者和 JAXB 2 (JSR-222 )专家组。
EclipseLink JAXB (MOXy) 提供本机 JSON 绑定支持。它将正确编组包装为 JSON 数组的大小为 1 的集合。下面是一个完整的例子。
公司
行业
jaxb.properties
为了将 MOXy 指定为您的 JAXB 提供商,您需要添加一个名为
jaxb.properties 的文件code> 与您的域类位于同一包中,并包含以下条目:
演示
输出
以下是运行演示代码的输出:
了解更多信息
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
EclipseLink JAXB (MOXy) provides native JSON-binding support. It will correctly marshal collections of size 1 wrapped as a JSON array. Below is a complete example.
Company
Industry
jaxb.properties
In order to specify MOXy as your JAXB provider you need to add a file called
jaxb.properties
in the same package as your domain classes with the following entry:Demo
Output
Below is the output from running the demo code:
For More Information
我对此不太确定,但尝试删除 industryList 的
@XMLElement
注释我已经做了相反的事情:使用 jaxb 从 xsd 模式生成 java 类文件。我查看了带有集合字段的生成的类,它们没有任何特定的注释。
另外你可能想尝试 JSON Lib: http://json-lib.sourceforge.net/
你可以执行以下操作:
这将生成 json 字符串,该字符串将包含例如复杂类型的集合。
然后,您可以使用 HttpServletResponse 等发送 jsonString。
我建议序列化 DTO 对象,而不是序列化实体对象。
I'm not too sure about this, but try removing the
@XMLElement
annotation for industryListI have done stuff the other way around: using jaxb to generate java classes from xsd schema files. I've looked at the generated classes with collection fields, and they don't have any specific annotations on them.
Also you may wanna try JSON Lib: http://json-lib.sourceforge.net/
you could do things like:
which will generate json string that will incorporate e.g. collections of complex types.
You could then send the jsonString using e.g. HttpServletResponse.
I would recommend serializing DTO objects rather than serialize your entity objects.
深入 JSONObject 并换出需要为 JSONArray 的区域。创建后,json putOpt 将用新的引用对象替换旧的引用对象。
之前
"投资": {"子账户": {
"id": "SubAccountId_2_CORP",
"分配百分比": "100.0",
“产品代码”:“PC01”,
"ProductFullName": "产品全名"
}}
之后
"投资": {"子账户": [{
"id": "SubAccountId_2_CORP",
"分配百分比": "100.0",
“产品代码”:“PC01”,
"ProductFullName": "产品全名"
}]}
。
。
。
。
Drill down JSONObject and swap out an area that needs to be JSONArray. Once created, json putOpt will replace old reference object with new.
Before
"Investment": {"SubAccount": {
"id": "SubAccountId_2_CORP",
"AllocPercent": "100.0",
"ProductCode": "PC01",
"ProductFullName": "Product Full Name"
}}
After
"Investment": {"SubAccount": [{
"id": "SubAccountId_2_CORP",
"AllocPercent": "100.0",
"ProductCode": "PC01",
"ProductFullName": "Product Full Name"
}]}
.
.
.
.