栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Jackson或其替代品将JSON树解析为普通类

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用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<String, Object> properties = new HashMap<String, Object>(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"   }}

想要查询更多的信息

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516089.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号