我正在使用以下库:
com.jayway.JsonPath
version 2.4.0
我正在使用共享的 JsonPath(过滤)表达式处理一个 json 字符串,并且我需要同时将不同的值更新到不同的匹配 json 节点,因为我需要所有匹配的绝对路径的列表。
但是我使用了这个 scala 代码,输出很接近但不是预期的:
val conf = Configuration.builder().options(com.jayway.jsonpath.Option.AS_PATH_LIST).build()
val pathList: net.minidev.json.JSONArray = JsonPath.using(conf).parse(str).read("$.vehicles[*].registrations.[?(@.name=='regNo')].value")
但这会返回:
["$['vehicles'][0]['registrations'][0]['value']","$['vehicles'][0]['registrations'][2]['value']","$['vehicles'][0]['registrations'][3]['value']"]
下面是输入场景:
输入Json
{
"id": "123",
"coverageDate": "2017-11-22",
"vehicles": [
{
"terms": {
"type": "Debit",
"holder": {
"name": "Orix Asia Ltd"
}
},
"owned": false,
"registrations": [
{
"name": "regNo",
"value": "ABC123",
"price": 12.22
},
{
"name": "abcNo",
"value": "DEF123s",
"price": 13.23
},
{
"name": "regNo",
"value": "XYZ123",
"price": 12.22
},
{
"name": "regNo",
"value": "ABC123",
"price": 12.22
},
{
"name": "customNo",
"value": "DEF123s"
}
],
"detail": {
"region": "Custom"
}
}
]
}
给定 JsonPath 过滤表达式:
$.vehicles[*].registrations.[?(@.name=='regNo')].value
查询:如何列出所有匹配的绝对路径,如下所示:
$.vehicles[0].registrations[0].value
$.vehicles[0].registrations[2].value
$.vehicles[0].registrations[3].value