使用Marshaller将Java对象转换为Json [英] Converting Java Object to Json using Marshaller

查看:230
本文介绍了使用Marshaller将Java对象转换为Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Marshaller将java对象转换为XML非常容易。但我需要单独使用marshaller将java对象转换为JSON。我知道使用gson或Xstream就好了。但是我需要使用Marshaller。如何实现呢?

Its quite easy to convert a java object to XML by using Marshaller. But I need to convert a java object to JSON by using marshaller alone. I know its good to use gson or Xstream like things., but I need to do using Marshaller.How to achieve it?

提前致谢。

推荐答案

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

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

以下是这个如果您使用MOXy作为JAXB提供程序,则可以完成。

Below is how this can be done if you are using MOXy as your JAXB provider.

  • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html

JAVA模型

客户

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

@XmlRootElement(namespace="http://www.example.com")
@XmlType(namespace="http://www.example.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    @XmlAttribute
    private int id;

    @XmlElement(namespace="http://www.example.com")
    private String firstName;

    @XmlElement(namespace="http://www.example.com", nillable=true)
    private String lastName;

    @XmlElement(namespace="http://www.example.com")
    private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();

}

PhoneNumber

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {

    @XmlAttribute
    private String type;

    @XmlValue
    private String number;

}

jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域名相同的包中包含名为 jaxb.properties 的文件具有以下条目的模型(请参阅: http:// blog .bdoughan.com / 2011/05 / specify-eclipselink-moxy-as-your.html

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: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)

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

演示代码

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<ns0:customer xmlns:ns0="http://www.example.com" id="123">
   <ns0:firstName>Jane</ns0:firstName>
   <ns0:lastName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
   <ns0:phoneNumbers type="work">555-1111</ns0:phoneNumbers>
</ns0:customer>

演示

在下面的演示代码中,我们将使用相同的JAXB元数据将XML文档转换为Java对象,然后将这些对象转换回JSON。使用MOXy,您可以通过在 Marshaller 上设置属性来指定JSON输出。

In the demo code below we will use the same JAXB metadata to convert an XML document to Java objects, and then convert those objects back to JSON. With MOXy you can specify JSON output by setting a property on the Marshaller.

import java.io.File;
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(Customer.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum15357366/input.xml");
        Customer customer = (Customer) unmarshaller.unmarshal(xml)
                ;
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        marshaller.marshal(customer, System.out);
    }

}

JSON输出

以下是JSON输出。请注意,没有与命名空间或XML属性对应的指示符。另请注意,大小为1的集合被正确表示为JSON数组(其他一些方法存在问题)。

Below is the JSON output. Note how there are no indicators corresponding to namespaces or XML attributes. Also note the collection of size one was correctly represented as a JSON array (a problem with some other approaches).

{
   "id" : 123,
   "firstName" : "Jane",
   "lastName" : null,
   "phoneNumbers" : [ {
      "type" : "work",
      "value" : "555-1111"
   } ]
}

这篇关于使用Marshaller将Java对象转换为Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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