【问题标题】:Parse JSON tree to plain class using Jackson or its alternatives使用 Jackson 或其替代方法将 JSON 树解析为普通类
【发布时间】:2013-03-17 14:22:33
【问题描述】:

如何解析该 JSON:

{
    "foo": {
        "bar": {
            "baz": "Hello"
        },
        "qux": "World"
    }
}

使用Jackson 或其替代方法进入该类:

public class Foo {
    private String baz;
    private String qux;

    public String getBaz() {
        return baz;
    }

    public void setBaz(final String baz) {
        this.baz = baz;
    }

    public String getQux() {
        return qux;
    }

    public void setQux(final String qux) {
        this.qux = qux;
    }
}

期待类似:

@JsonProperty("foo.bar.baz")
private String baz;
@JsonProperty("foo.qux")
private String qux;

【问题讨论】:

标签: java json jackson


【解决方案1】:

我发现,Jackson 中还没有实现此功能,请参阅issue。

作为一种解决方法,可以将以下方法添加到Foo 类中:

@JsonProperty("foo")
public void setFoo(JsonNode jsonNode) {
    this.qux = jsonNode.get("qux").getTextValue();
    this.baz = jsonNode.get("bar").get("baz").getTextValue();
}

【讨论】:

    【解决方案2】:

    注意:我是EclipseLink JAXB (MOXy) 领导,也是JAXB (JSR-222) 专家组的成员。

    此用例可能不适用于 Jackson,但当 MOXy 用作您的 JSON 绑定提供程序时可以完成。

    Foo

    您可以在此用例中利用 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/Output

    {
       "foo" : {
          "bar" : {
             "baz" : "Hello"
          },
          "qux" : "World"
       }
    }
    

    更多信息

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 2012-11-08
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多