Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jackson解析深度嵌套的JSON属性_Java_Json_Jackson_Mapping_Json Deserialization - Fatal编程技术网

Java 使用Jackson解析深度嵌套的JSON属性

Java 使用Jackson解析深度嵌套的JSON属性,java,json,jackson,mapping,json-deserialization,Java,Json,Jackson,Mapping,Json Deserialization,我试图找到一种从API的有效负载解析嵌套属性的干净方法 以下是对JSON有效负载的粗略概括: { "root": { "data": { "value": [ { "user": { "id": "1", "name": { "first": "x", "last": "y" } }

我试图找到一种从
API
的有效负载解析嵌套属性的干净方法

以下是对
JSON
有效负载的粗略概括:

{
  "root": {
    "data": {
      "value": [
        {
          "user": {
            "id": "1",
            "name": {
              "first": "x",
              "last": "y"
            }
          }
        }
      ]
    }
  }
}
我的目标是拥有一个
User
对象数组,其中包含
firstName
lastName
字段

有人知道一个干净地解析这个的好方法吗

现在,我正在尝试创建一个
包装器
类,其中包含用于数据、值、用户等的静态内部类。但这似乎只是为了读取第一个/最后一个属性数组

我正在使用
restemplate.exchange()
调用端点。

您需要使用库,它只允许您选择所需字段,然后您可以使用
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<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 + '\'' +
                '}';
    }
}

还有一种使用lib org.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);

如果你有时间,你能再详细一点吗?您是否只是试图获取名字和姓氏值,而忽略其余的值?是否要忽略应用程序代码中的根/数据对象结构的其余部分?我需要一个包含名和姓的
User
对象数组。也就是说,在上面的JSON中,我只想提取
first
last
属性。我明白了,其他一切都应该被忽略。Jackson可能有办法做到这一点,但我猜您必须按原样反序列化整个json字符串,并从root->data-value->等获取完整的对象图。。。然后使用一些代码将完整的对象图映射到所需的数据模型中。根据我的观察,这是一个常见的过程,也是使用远程API的常见副作用之一。请具体说明,
reader
来自哪里?我写道我使用了这个-org.json.simple-library
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);