我是Java编程新手,我正在开发一个带有REST服务的Spring Boot应用程序,该服务将调用另一个服务并返回JSON响应。 我正在使用OkHttpClient来处理这个调用。
但是,从JSON响应中,我只需要很少的属性作为List格式的最终输出。
如何从okHttpCliwnt响应中仅提取所需的属性?
我对第三方服务的回复如下:
{
"employeeDetail": [{
"employee": {
"name": "abc",
"age": "30",
"details": {
"role": "developer",
"phone": "123"
}
}
},
{
"employee": {
"name": "abc",
"age": "30",
"details": {
"role": "sr.developer",
"phone": "1234"
}
}
}
]
}
从这个回复中,我的最终回复只需要如下:
{
"employeeDetail": [{
"name": "abc",
"age": "30",
"role": "developer"
},
{
"name": "abc",
"age": "30",
"role": "sr.developer"
}
]
}
请帮助我。
答案 0 :(得分:0)
我不知道如何将JSON转换为List<>但您可以使用Gson
将JSON转换为Java对象。
之后,您可以将对象或对象本身的内容添加到列表中。
这是https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
的摘录Gson gson = new Gson();
// 1. JSON to Java object, read it from a file.
Staff staff = gson.fromJson(new FileReader("D:\\file.json"), Staff.class);
// 2. JSON to Java object, read it from a Json String.
String jsonInString = "{'name' : 'mkyong'}";
Staff staff = gson.fromJson(jsonInString, Staff.class);
// JSON to JsonElement, convert to String later.
JsonElement json = gson.fromJson(new FileReader("D:\\file.json"),
JsonElement.class);
String result = gson.toJson(json);
答案 1 :(得分:0)
我搜索但是为了这样的筑巢,我找不到任何具体的东西。然而,我尝试了JsonNode,我得到了这个。
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(str);
JsonNode empDetNode = rootNode.path("employeeDetail");
Iterator<JsonNode> elements = empDetNode.elements();
List<Employee> empList = new ArrayList<Employee>();
Gson gson = new Gson();
while (elements.hasNext()) {
Employee emp1 = new Employee();
JsonNode emp= elements.next();
JsonNode empl= emp.path("employee");
JsonNode name= empl.path("name");
JsonNode age= empl.path("age");
JsonNode details= empl.path("details");
JsonNode role= details.path("details");
emp1.setAge(age.toString());
emp1.setName(name.toString());
emp1.setRole(role.toString());
empList.add(emp1);
}
EmpDetl empdetl = new EmpDetl();
empdetl.setEmployeeDetl(empList);
public class Employee {
private String name;
private String age;
private String role;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
import java.util.List;
public class EmpDetl {
private List<Employee> employeeDetl;
public List<Employee> getEmployeeDetl() {
return employeeDetl;
}
public void setEmployeeDetl(List<Employee> empLists) {
this.employeeDetl = empLists;
}
@Override
public String toString() {
return "EmpDetl [empLists=" + employeeDetl + "]";
}
}
答案 2 :(得分:0)
杰克逊可能是您正在寻找的工具。你只需要创建一个类,比如说Employee.java:
public class Employee {
@JsonProperty("name")
private String name;
@JsonProperty("age")
private String age;
@JsonProperty("role")
private String role;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
和EmployeeDetail.java
import java.util.List;
@JsonRootName(value = "employeeDetail")
public class EmployeeDetail {
private List<Employee> employees;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
更多注释,请refer
答案 3 :(得分:-1)
您必须使用org.json或其他一些json框架手动进行解析。