【问题标题】:Use JsonPath on flattened json在扁平化的 json 上使用 JsonPath
【发布时间】:2021-06-30 14:15:03
【问题描述】:

我想使用 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 示例工作?

作为附加问题。有没有办法通过read() 方法获得完整的json?或者是通过调用read() 两次(一个带有Option.AS_PATH_LIST 和一个不带此选项)获得“完整JSON”并将这两个结果合并到一个像上面一样的flot 列表的唯一方法,最后一步将它展平得到一个“正常”的 JSON?

【问题讨论】:

    标签: java json jsonpath json-flattener


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 1970-01-01
      • 2021-05-24
      • 2011-11-15
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      • 2019-04-24
      相关资源
      最近更新 更多