如何使用 resttemplate 获取 JSON 数组
How get JSON array with resttemplate
API 回应
{
"status": "success",
"data": [
{
"id": "1",
"employee_name": "Tiger Nixon",
"employee_salary": "320800",
"employee_age": "61",
"profile_image": ""
},
...
]
}
public class数据{
private int id;
private String employeeName;
private int employeeSalary;
private int employeeAge;
private String profileImage;
}
Setter getter 并且构造函数已省略
public class 员工 {
private String status;
private Data[] datas;
}
public Employees getProductList() {
ResponseEntity<Employees> result = restTemplate.exchange("http://dummy.restapiexample.com/api/v1/employees",
HttpMethod.GET, null, Employees.class);
return result.getBody();
}
这是返回
员工{status=success, datas=null}
为什么数据为空?
感谢您的帮助:)
请将您的字段注释为 @JsonProperty
和字段名称以匹配输入 json:
public class Data {
@JsonProperty
private String id;
@JsonProperty
private String employee_name;
@JsonProperty
private String employee_salary;
@JsonProperty
private String employee_age;
@JsonProperty
private String profile_image;
}
public class Employees {
@JsonProperty
private String status;
@JsonProperty
private List<Data> data;
}
API 回应
{
"status": "success",
"data": [
{
"id": "1",
"employee_name": "Tiger Nixon",
"employee_salary": "320800",
"employee_age": "61",
"profile_image": ""
},
...
]
}
public class数据{
private int id;
private String employeeName;
private int employeeSalary;
private int employeeAge;
private String profileImage;
}
Setter getter 并且构造函数已省略
public class 员工 {
private String status;
private Data[] datas;
}
public Employees getProductList() {
ResponseEntity<Employees> result = restTemplate.exchange("http://dummy.restapiexample.com/api/v1/employees",
HttpMethod.GET, null, Employees.class);
return result.getBody();
}
这是返回
员工{status=success, datas=null}
为什么数据为空?
感谢您的帮助:)
请将您的字段注释为 @JsonProperty
和字段名称以匹配输入 json:
public class Data {
@JsonProperty
private String id;
@JsonProperty
private String employee_name;
@JsonProperty
private String employee_salary;
@JsonProperty
private String employee_age;
@JsonProperty
private String profile_image;
}
public class Employees {
@JsonProperty
private String status;
@JsonProperty
private List<Data> data;
}