gpt4 book ai didi

java - Jackson 中的 @XmlPath 模拟是什么?

转载 作者:行者123 更新时间:2023-12-02 01:34:34 28 4
gpt4 key购买 nike

使用org.eclipse.persistence.oxm.annotations.XmlPath可以read相同的元素放入不同的类字段中。有 jackson 的模拟注释吗?

最佳答案

模拟是 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

关于java - Jackson 中的 @XmlPath 模拟是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42441502/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com