杰克逊的@XmlPath模拟是什么?

时间:2017-02-24 14:46:10

标签: java xml xpath jackson moxy

使用org.eclipse.persistence.oxm.annotations.XmlPath,可以将read个相同的元素放入单独的类字段中。它有杰克逊的模拟注释吗?

1 个答案:

答案 0 :(得分:0)

模拟是Elastic Path's JSON Unmarshaller

考虑到关于XmlPath的问题中的示例,我创建了以下示例:

输入Json

{ "response": 
  { "employee": [ 
    {"name":"Sharique"}, 
    {"name":24}, 
    {"name":"India"}
  ]}
}

目标POJO:

public class Employee
{
    @JsonPath("$.response.employee[0].name")
    private String empName;
    @JsonPath("$.response.employee[1].name")
    private int age;
    @JsonPath("$.response.employee[2].name")
    private String country;

    public String getEmpName() { return empName; }
    public void   setEmpName(String empName) { this.empName = empName; }
    public int    getAge() { return age; }
    public void   setAge(int age) { this.age = age; }
    public String getCountry() { return country; }
    public void   setCountry(String country) { this.country = country; }
}

调用:

public static void main(String[] args)
{
    String json = "{ \"response\": { \"employee\": [ {\"name\":\"Sharique\"}, {\"name\":24}, {\"name\":\"India\"}] }}";

    try {
        JsonUnmarshaller unmarshaller = new DefaultJsonUnmarshaller();
        Employee emp = unmarshaller.unmarshal(Employee.class, json);
        System.out.println(emp.getEmpName() + " " + emp.getAge() + " " + emp.getCountry());
    } catch (Exception e) {
        e.printStackTrace();
    }

输出:

Sharique 24 India