我想使用 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
一个没有这个选项)并将这两个结果合并到一个浮动列表中,就像上面一样,最后一步将它展平以获得“正常”JSON ?