天天看點

jackson json 嵌套對象_用Jackson解析深度嵌套的JSON屬性

小編典典

您需要使用JsonPath庫,該庫僅允許您選擇必填字段,然後可以Jackson将原始資料轉換為POJO類。解決方案示例如下所示:

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 nodes = JsonPath.parse(jsonFile).read("$..value[*].user.name");

ObjectMapper mapper = new ObjectMapper();

CollectionType usersType = mapper.getTypeFactory().constructCollectionType(List.class, User.class);

List 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'}]

2020-07-27