java jettison_java-Jettison或Kryo

注意:我是EclipseLink JAXB (MOXy)主管,并且是JAXB (JSR-222)专家组的成员.

由于您已经建立了JAXB映射并将XML转换为JSON,因此您可能会对EclipseLink JAXB(MOXy)感兴趣,它使用相同的JAXB元数据提供了对象到XML和对象到JSON的映射.

顾客

下面是带有JAXB批注的示例模型.

package forum11599191;

import java.util.List;

import javax.xml.bind.annotation.*;

@XmlRootElement

@XmlAccessorType(XmlAccessType.FIELD)

public class Customer {

@XmlAttribute

private int id;

private String firstName;

@XmlElement(nillable=true)

private String lastName;

private List email;

}

jaxb.properties

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

input.xml

Jane

jdoe@example.com

演示版

以下演示代码将从XML中填充对象,然后输出JSON.请注意,MOXy上没有编译时间相关性.

package forum11599191;

import java.io.File;

import javax.xml.bind.*;

public class Demo {

public static void main(String[] args) throws Exception {

JAXBContext jc = JAXBContext.newInstance(Customer.class);

// Unmarshal from XML

Unmarshaller unmarshaller = jc.createUnmarshaller();

File xml = new File("src/forum11599191/input.xml");

Customer customer = (Customer) unmarshaller.unmarshal(xml);

// Marshal to JSON

Marshaller marshaller = jc.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.setProperty("eclipselink.media-type", "application/json");

marshaller.marshal(customer, System.out);

}

}

JSON输出

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

{

"customer" : {

"id" : 123,

"firstName" : "Jane",

"lastName" : null,

"email" : [ "jdoe@example.com" ]

}

}

关于输出的一些注意事项:

>由于id字段是数字类型,因此将其编组为JSON,且不带引号.

>即使id字段是使用@XmlAttribute映射的,在JSON消息中也没有对此的特殊指示.

> email属性的大小为1,这在JSON输出中正确表示.

> xsi:nil机制用于指定lastName字段具有null值,该值已转换为JSON输出中的正确null表示形式.

欲获得更多信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值