java json替换_java-使用Jackson或其替代方法将JSON树解析为...

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

Jackson可能无法使用此用例,但将MOXy用作JSON绑定提供程序时可以完成.

oo

您可以在此用例中利用MOXy基于路径的映射.

import org.eclipse.persistence.oxm.annotations.XmlPath;

public class Foo {

private String baz;

private String qux;

@XmlPath("foo/bar/baz/text()")

public String getBaz() {

return baz;

}

public void setBaz(final String baz) {

this.baz = baz;

}

@XmlPath("foo/qux/text()")

public String getQux() {

return qux;

}

public void setQux(final String qux) {

this.qux = qux;

}

}

演示版

JAXB运行时API用于读取/写入JSON.

import java.util.*;

import javax.xml.bind.*;

import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

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

Map properties = new HashMap(2);

properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");

properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);

JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class}, properties);

Unmarshaller unmarshaller = jc.createUnmarshaller();

StreamSource json = new StreamSource("src/forum15659950/input.json");

Foo foo = unmarshaller.unmarshal(json, Foo.class).getValue();

Marshaller marshaller = jc.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.marshal(foo, System.out);

}

}

input.json /输出

{

"foo" : {

"bar" : {

"baz" : "Hello"

},

"qux" : "World"

}

}

欲获得更多信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值