使用 Jackson 解析深度嵌套的 JSON 属性
Parsing deeply nested JSON properties with Jackson
我正在尝试找到一种从 API
.
的负载中解析嵌套属性的简洁方法
这里是 JSON
有效负载的粗略概括:
{
"root": {
"data": {
"value": [
{
"user": {
"id": "1",
"name": {
"first": "x",
"last": "y"
}
}
}
]
}
}
}
我的目标是拥有一个包含 User
个对象的数组,其中包含 firstName
和 lastName
个字段。
有谁知道清楚地解析它的好方法吗?
现在我正在尝试创建一个 Wrapper
class 并且其中有用于数据、值、用户等的静态内部 classes 但这似乎是一种混乱的方式这样做只是为了读取 first/last 属性数组。
我正在使用 restTemplate.exchange()
调用端点。
JsonPath 库只允许 select 必填字段,然后您可以使用 Jackson
将原始数据转换为 POJO
class。示例解决方案如下所示:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.jayway.jsonpath.JsonPath;
import java.io.File;
import java.util.List;
import java.util.Map;
public class JsonPathApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
List<Map> nodes = JsonPath.parse(jsonFile).read("$..value[*].user.name");
ObjectMapper mapper = new ObjectMapper();
CollectionType usersType = mapper.getTypeFactory().constructCollectionType(List.class, User.class);
List<User> users = mapper.convertValue(nodes, usersType);
System.out.println(users);
}
}
class User {
@JsonProperty("first")
private String firstName;
@JsonProperty("last")
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "User{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
以上代码打印:
[User{firstName='x', lastName='y'}]
另一个简单的方法是使用 JSON.simple 库:
JSONParser jsonParser = new JSONParser();
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONObject jObj = (JSONObject) obj;
JSONObject root = (JSONObject)jObj.get("root");
JSONObject data = (JSONObject) root.get("data");
JSONArray value = (JSONArray) data.get("value");
JSONObject array = (JSONObject) value.get(0);
JSONObject user = (JSONObject) array.get("user");
JSONObject name = (JSONObject) user.get("name");
String lastName = (String) name.get("last");
String firstName = (String) name.get("first");
System.out.println(lastName + " " + firstName);
我正在尝试找到一种从 API
.
这里是 JSON
有效负载的粗略概括:
{
"root": {
"data": {
"value": [
{
"user": {
"id": "1",
"name": {
"first": "x",
"last": "y"
}
}
}
]
}
}
}
我的目标是拥有一个包含 User
个对象的数组,其中包含 firstName
和 lastName
个字段。
有谁知道清楚地解析它的好方法吗?
现在我正在尝试创建一个 Wrapper
class 并且其中有用于数据、值、用户等的静态内部 classes 但这似乎是一种混乱的方式这样做只是为了读取 first/last 属性数组。
我正在使用 restTemplate.exchange()
调用端点。
JsonPath 库只允许 select 必填字段,然后您可以使用 Jackson
将原始数据转换为 POJO
class。示例解决方案如下所示:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.jayway.jsonpath.JsonPath;
import java.io.File;
import java.util.List;
import java.util.Map;
public class JsonPathApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
List<Map> nodes = JsonPath.parse(jsonFile).read("$..value[*].user.name");
ObjectMapper mapper = new ObjectMapper();
CollectionType usersType = mapper.getTypeFactory().constructCollectionType(List.class, User.class);
List<User> users = mapper.convertValue(nodes, usersType);
System.out.println(users);
}
}
class User {
@JsonProperty("first")
private String firstName;
@JsonProperty("last")
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "User{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
以上代码打印:
[User{firstName='x', lastName='y'}]
另一个简单的方法是使用 JSON.simple 库:
JSONParser jsonParser = new JSONParser();
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONObject jObj = (JSONObject) obj;
JSONObject root = (JSONObject)jObj.get("root");
JSONObject data = (JSONObject) root.get("data");
JSONArray value = (JSONArray) data.get("value");
JSONObject array = (JSONObject) value.get(0);
JSONObject user = (JSONObject) array.get("user");
JSONObject name = (JSONObject) user.get("name");
String lastName = (String) name.get("last");
String firstName = (String) name.get("first");
System.out.println(lastName + " " + firstName);