如何获取 Jayway 返回的数组 [String] 中 child 的路径
How to get path for child in Array[String] returned by Jayway
我嵌套了 json 数据,像这样:
{
"libraries":[
{
"State":"California",
"genres":[
{
"genre":"thriller",
"books":[
{
"title":"Book 1"
},
{
"title":"Book 2"
}
]
},
{
"genre":"mystery",
"books":[
{
"title":"Book 2"
},
{
"title":"Book 3"
}
]
}
]
},
{
"State":"New York",
"genres":[
{
"genre":"horror",
"books":[
{
"title":"Book 2"
},
{
"title":"Book 5"
}
]
},
{
"genre":"comedy",
"books":[
{
"title":"Book 6"
},
{
"title":"Book 7"
}
]
}
]
}
]
}
我正在使用 Scala 中的 jayway jsonpath 库来解析它。
我可以使用 JsonPath.read(myData,"$.libraries[*].genres[*].books[*]")
之类的东西来获取从每个图书馆汇集的所有书籍的数组。我想要的是知道每本书的路径,例如"$.libraries(0).genres(1).books(0)"
。有没有办法获取所有书籍路径的数组,而不仅仅是所有书籍?
总的来说,我是 json 路径的新手,所以如果这不是一个好问题,请原谅我。
您可以使用 Option.AS_PATH_LIST
的配置:
val conf = Configuration.builder.options(Option.AS_PATH_LIST).build
val paths = JsonPath.parse(json, conf).read[JSONArray]("$.libraries[*].genres[*].books[*]")
for(path <- paths.toArray){
println(path.toString)
}
以你的例子为例 json:
$['libraries'][0]['genres'][0]['books'][0]
$['libraries'][0]['genres'][0]['books'][1]
$['libraries'][0]['genres'][1]['books'][0]
$['libraries'][0]['genres'][1]['books'][1]
$['libraries'][1]['genres'][0]['books'][0]
$['libraries'][1]['genres'][0]['books'][1]
$['libraries'][1]['genres'][1]['books'][0]
$['libraries'][1]['genres'][1]['books'][1]
我嵌套了 json 数据,像这样:
{
"libraries":[
{
"State":"California",
"genres":[
{
"genre":"thriller",
"books":[
{
"title":"Book 1"
},
{
"title":"Book 2"
}
]
},
{
"genre":"mystery",
"books":[
{
"title":"Book 2"
},
{
"title":"Book 3"
}
]
}
]
},
{
"State":"New York",
"genres":[
{
"genre":"horror",
"books":[
{
"title":"Book 2"
},
{
"title":"Book 5"
}
]
},
{
"genre":"comedy",
"books":[
{
"title":"Book 6"
},
{
"title":"Book 7"
}
]
}
]
}
]
}
我正在使用 Scala 中的 jayway jsonpath 库来解析它。
我可以使用 JsonPath.read(myData,"$.libraries[*].genres[*].books[*]")
之类的东西来获取从每个图书馆汇集的所有书籍的数组。我想要的是知道每本书的路径,例如"$.libraries(0).genres(1).books(0)"
。有没有办法获取所有书籍路径的数组,而不仅仅是所有书籍?
总的来说,我是 json 路径的新手,所以如果这不是一个好问题,请原谅我。
您可以使用 Option.AS_PATH_LIST
的配置:
val conf = Configuration.builder.options(Option.AS_PATH_LIST).build
val paths = JsonPath.parse(json, conf).read[JSONArray]("$.libraries[*].genres[*].books[*]")
for(path <- paths.toArray){
println(path.toString)
}
以你的例子为例 json:
$['libraries'][0]['genres'][0]['books'][0]
$['libraries'][0]['genres'][0]['books'][1]
$['libraries'][0]['genres'][1]['books'][0]
$['libraries'][0]['genres'][1]['books'][1]
$['libraries'][1]['genres'][0]['books'][0]
$['libraries'][1]['genres'][0]['books'][1]
$['libraries'][1]['genres'][1]['books'][0]
$['libraries'][1]['genres'][1]['books'][1]