如何通过将所有路径作为参数传递来获取JSON字段

时间:2020-04-21 11:42:52

标签: java json

我有一个json:

"employee": {
    "firstName": "Brian",
    "lastName": "Schultz",
    "website": "example.com"
}

在杰克逊获得“名字”时,我必须这样做:

JsonNode idNode = rootNode.path("employee").path("firstName");

我想做这样的事情:path("employee/firstName")

我在Jackson课堂上没有看到“ at”方法。

您能告诉我如何执行此操作,还是告诉我另一个库而不是杰克逊?

1 个答案:

答案 0 :(得分:0)

我建议使用jsonpath

有两个库可供使用:

  1. 独立(https://github.com/json-path/JsonPath
  2. 部分RestAssured(https://github.com/rest-assured/rest-assured/wiki/GettingStarted#jsonpath

以防万一,您不想使用相同的方式来查询XML和JSON,我建议使用第一个/