在扁平化的 json

时间:2021-06-30 14:15:03

标签: java json jsonpath json-flattener

我想使用 jsonpath 来搜索扁平 JSON 中的项目。我目前使用这个 maven 包:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>

从外部来源我得到一个扁平的 json,类似于这个(缩短):

 "isactive",true
 "article","somemoretester"
 "name.title","Mr"
 "name.first","Corey"
 "name.last","Duncan"
 "name.name","Mr. Corey Duncan"
 "location.street.number",3895
 "location.street.name","Mockingbird Ln"
 "location.city","Birmingham"
 "location.state","Florida"
 "location.timezone.offset","0:00"
 "location.timezone.description","Western Europe Time, London, Casablanca"
 "location.timezone.name","TeaTime"
 "location.timezone.prename","NoTea"
 "location.details.name","B-City"
 "location.details.infratrurcture.publictransport.available",true
 "location.details.infratrurcture.publictransport.name","Birmingtrain"
 "location.details.infratrurcture.publictransport.lines[0].id","B1"
 "location.details.infratrurcture.publictransport.lines[0].name","GreenLine"
 "location.details.infratrurcture.publictransport.lines[0].stations",12
 "location.details.infratrurcture.publictransport.lines[1].id","B2"
 "location.details.infratrurcture.publictransport.lines[1].name","BlueLine"
 "location.details.infratrurcture.publictransport.lines[1].stations",5
 "location.details.infratrurcture.major.name","Mr. Phil Lazio"
 "email","corey.duncan@example.com"
 "login.uuid","33768122-74a6-4457-89ec-e5adeb8c179e"
 "login.username","tinypeacock919"
 "dob.date","1964-07-30T06:19:04.561Z"
 "dob.age",57
 "dob.name","Birthday"
 "nat","US"
 "tags[0][0]","name"
 "tags[0][1]","nat"
 "tags[0][2]","number"
 "tags[1][0]","ibu"
 "tags[1][1]","infratrurcture"
 "tags[1][2]","isactive"
 "tags[2][0]","cell"
 "tags[2][1]","coordinates"
 "tags[2][2]","country"
 "tags[3][0]","id"
 "tags[3][1]","irrelevant"
 "tags[3][2]","infos"
 "secrets[0][0][0].name","example1"
 "secrets[0][0][0].value","nothing"
 "secrets[0][0][1].name","example2"
 "secrets[0][0][1].value","same"
 "secrets[0][1][0].name","oof"
 "secrets[0][1][0].value","ofo"
 "secrets[0][1][1].name","java"
 "secrets[0][1][1].value","nojava"
 "secrets[0][2][0].name","stuff"
 "secrets[0][2][0].value","things"
 "secrets[0][2][1].name","foo"
 "secrets[0][2][1].value","bar"

有没有办法将这个扁平化的输入与 json-path 库一起使用?我试过类似的东西:

JsonPath.parse(flattenedjson, Configuration.defaultConfiguration()).read("$..name")

但是没有结果。如果我使用 JSON-unflattener 并使用:

JsonPath.parse(JsonUnflattener.unflatten(flattenedjson), Configuration.defaultConfiguration()).read("$..name")

它会像魅力一样发挥作用......但是这个不平坦的步骤会花费很多时间。有什么办法可以让我的第一个 cvode 示例工作吗?

作为附加问题。有没有办法获得完整的 json 作为 read() 方法的结果?或者是通过两次调用 read() 获得“完整的 JSON”的唯一方法(一次使用 Option.AS_PATH_LIST,另一次不使用此选项)并将这两个结果合并到一个浮点列表中,如上面的 unflatten it作为获得“正常”JSON 的最后一步?

1 个答案:

答案 0 :(得分:0)

扁平版本的部分是字符串。这些是 JSON 中的合法键,因此在使用大多数工具时,您必须将这些键指定为扁平状态。

在展示时使用“unflattener”可能是最好的选择。

关于你的其他问题,我对这个库不是特别熟悉,所以我恐怕帮不上什么忙。