JAXB / Jackson 处理数组
我目前正在为 Web 应用程序编写 Jersey REST 接口。我想要的是能够将 JSON 和 XML 请求序列化到同一对象,但是我无法让 Jersey(即 JAXB / Jackson)以我想要的格式使用 XML 和 JSON。
XML 结构如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<message>
<buckets>
<bucket>
<channels>
<channel>Test A</channel>
<channel>Test B</channel>
</channels>
<text>This is sample text</text>
</bucket>
<bucket>
<channels>
....
</channels>
<text>This is sample text</text>
</bucket>
</buckets>
<userId>10</userId>
</message>
类如下所示:
@XmlRootElement(name="message")
public class MultiMessageRS {
public static class Bucket {
private List<String> channels;
private String text;
public Bucket () {}
@XmlElementWrapper(name="channels")
@XmlElement(name="channel")
public List<String> getChannels() {
return channels;
}
public void setChannels(List<String> channels) {
this.channels = channels;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
private List<Bucket> buckets;
private long userId;
public MultiMessageRS () {}
@XmlElementWrapper(name="buckets")
@XmlElement(name="bucket")
public List<Bucket> getBuckets() {
return buckets;
}
public void setBuckets(List<Bucket> buckets) {
this.buckets = buckets;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
}
这对于 XML 来说效果很好,但是它无法以我想要的格式使用 JSON。也就是说,问题在于它没有获取通道数组。格式如下:
{
"buckets":[
{
"bucket":{
"channels":[
"twitter",
"mobile"
],
"text":"This is sample text"
},
"bucket":{
"channels":[
"email",
"voice"
],
"text":"This is sample text"
}
}
],
"userId":"10"
}
为了能够正确地使用 JSON,我必须执行一些操作,例如在“channels”下添加一个“channel”数组,这是相当危险的。我该如何处理这种情况?
I'm currently writing a Jersey REST interface for a web application. What I want is the ability to be able to serialize a JSON and XML request to the same object however I'm having trouble getting the Jersey (i.e. JAXB / Jackson) to consume the XML and JSON in the format I want.
The XML structure looks like so:
<?xml version="1.0" encoding="UTF-8" ?>
<message>
<buckets>
<bucket>
<channels>
<channel>Test A</channel>
<channel>Test B</channel>
</channels>
<text>This is sample text</text>
</bucket>
<bucket>
<channels>
....
</channels>
<text>This is sample text</text>
</bucket>
</buckets>
<userId>10</userId>
</message>
The class looks like:
@XmlRootElement(name="message")
public class MultiMessageRS {
public static class Bucket {
private List<String> channels;
private String text;
public Bucket () {}
@XmlElementWrapper(name="channels")
@XmlElement(name="channel")
public List<String> getChannels() {
return channels;
}
public void setChannels(List<String> channels) {
this.channels = channels;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
private List<Bucket> buckets;
private long userId;
public MultiMessageRS () {}
@XmlElementWrapper(name="buckets")
@XmlElement(name="bucket")
public List<Bucket> getBuckets() {
return buckets;
}
public void setBuckets(List<Bucket> buckets) {
this.buckets = buckets;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
}
This works fine for the XML however it's unable to consume the JSON in the format I would like. Namely, the issue is that it isn't picking up the channels array. The format is as follows:
{
"buckets":[
{
"bucket":{
"channels":[
"twitter",
"mobile"
],
"text":"This is sample text"
},
"bucket":{
"channels":[
"email",
"voice"
],
"text":"This is sample text"
}
}
],
"userId":"10"
}
From it to work correctly with the JSON, I would have to do something like adding a 'channel' array under 'channels' which is pretty dodgy. How will I be able to handle this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:我是EclipseLink JAXB (MOXy) 的领导者和 JAXB 2 的成员(JSR-222)专家组。
EclipseLink JAXB (MOXy) 提供对 JSON 绑定的本机支持。下面的示例演示了您在问题中发布的模型的 XML 和 JSON 表示形式。
演示
input.xml
输出
了解更多信息
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
EclipseLink JAXB (MOXy) offers native support for JSON-binding. Below is an example demonstrating the XML and JSON representation of the model you posted in your question.
Demo
input.xml
Output
For More Information
我认为您的 JSON 过于复杂(并且通过重复相同的属性名称使其无效)。
看起来您可能想要使用的是这样的:
应该按预期工作。
I think you are overcomplicating your JSON (as well as making it invalid by repeating same property name).
It seems like what you might want to use is something like:
which should work as expected.