XML通过Jettison和JAXB将列表包装到JSON数组 [英] XML wrapped list to JSON array via Jettison and JAXB

查看:140
本文介绍了XML通过Jettison和JAXB将列表包装到JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB以以下形式将带注释的对象封送到XML:

I'm using JAXB to marshal an annotated object to XML in the form:

  <channels>
     <channel>Test A</channel>
     <channel>Test B</channel>
  </channels>

我想使用JAXB将此编组为JSON(ala http://blog.bdoughan.com/2011/04/jaxb-and-json-via-jettison.html)但它会编组如下:

I want to marshal this to JSON instead using JAXB (ala http://blog.bdoughan.com/2011/04/jaxb-and-json-via-jettison.html) but it marshals to something like the following:

  "channels" : {
    "channel" : [ "Test A", "Test B" ]
  },

我真的想要它编组成以下形式:

Really I want it to marshal into the following form:

  "channels" : {
    {"Test A"}, {"Test B"}
  },

我该怎么做?这是正确的做法吗?

How can I do this? Is it the right thing to do?

推荐答案

注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB 2(JSR-222) 专家组。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

以下是在EclipseLink JAXB(MOXy)中使用JSON绑定支持此用例的方法。

Below is how you could support this use case using the JSON-binding in EclipseLink JAXB (MOXy).

Java模型(根)

下面是我将用于此示例的Java模型。

Below is the Java model that I will use for this example.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Root {

    private List<String> channels = new ArrayList<String>();

    @XmlElementWrapper
    @XmlElement(name="channel")
    public List<String> getChannels() {
        return channels;
    }

}

指定MOXy为JAXB Provider(jaxb.properties)

要将MOXy指定为JAXB提供程序,您需要包含一个名为的文件jaxb.properties 在与您的域模型相同的包中,带有以下条目(请参阅:):

To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: ):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示代码

在下面的演示代码中,我们将同一个实例输出到XML和JSON。

In the demo code below we will output the same instance to both XML and JSON.

import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Root root = new Root();
        root.getChannels().add("Test A");
        root.getChannels().add("Test B");

        // Output XML
        marshaller.marshal(root, System.out);

        // Output JSON
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        marshaller.setProperty(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
        marshaller.marshal(root, System.out);
    }

}

输出

以下是运行演示代码的输出:

Below is the output from running the demo code:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <channels>
      <channel>Test A</channel>
      <channel>Test B</channel>
   </channels>
</root>



{
   "channels" : [ "Test A", "Test B" ]
}

更多信息

  • http://blog.bdoughan.com/2013/03/binding-to-json-xml-handling-collections.html

这篇关于XML通过Jettison和JAXB将列表包装到JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆