使用 jsonpath-plus 模块(打字稿),我尝试导航到 json 文档中的特定对象。目标对象向下几层,并且包括穿过 2 层数组。以下 jsonpath 语句:
$..['gmd:DQ_AbsoluteExternalPositionalAccuracy']
在在线jsonpath测试网站jsonpath.com返回如下结果:
[
{
"gmd:nameOfMeasure": {
"gco:CharacterString": "Difference to ICESat LE90"
},
"gmd:result": {
"gmd:DQ_QuantitativeResult": {
"gmd:valueUnit": {
"@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
},
"gmd:value": {
"gco:Record": {
"gco:Real": "0.655608"
}
}
}
}
},
{
"gmd:nameOfMeasure": {
"gco:CharacterString": "Difference to ICESat Vertical Mean"
},
"gmd:result": {
"gmd:DQ_QuantitativeResult": {
"gmd:valueUnit": {
"@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
},
"gmd:value": {
"gco:Record": {
"gco:Real": "-0.005536"
}
}
}
}
},
{
"gmd:nameOfMeasure": {
"gco:CharacterString": "Difference to ICESat Vertical RMSE"
},
"gmd:result": {
"gmd:DQ_QuantitativeResult": {
"gmd:valueUnit": {
"@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
},
"gmd:value": {
"gco:Record": {
"gco:Real": "0.398874"
}
}
}
}
},
{
"gmd:nameOfMeasure": {
"gco:CharacterString": "Difference to GCP LE90"
},
"gmd:result": {
"gmd:DQ_QuantitativeResult": {
"gmd:valueUnit": {
"@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
},
"gmd:value": {
"gco:Record": {
"gco:Real": "2.897789"
}
}
}
}
},
{
"gmd:nameOfMeasure": {
"gco:CharacterString": "Difference to GCP Vertical Mean"
},
"gmd:result": {
"gmd:DQ_QuantitativeResult": {
"gmd:valueUnit": {
"@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
},
"gmd:value": {
"gco:Record": {
"gco:Real": "-0.383740"
}
}
}
}
},
{
"gmd:nameOfMeasure": {
"gco:CharacterString": "Difference to GCP Vertical RMSE"
},
"gmd:result": {
"gmd:DQ_QuantitativeResult": {
"gmd:valueUnit": {
"@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
},
"gmd:value": {
"gco:Record": {
"gco:Real": "1.760134"
}
}
}
}
},
{
"gmd:nameOfMeasure": {
"gco:CharacterString": "Absolute horizontal accuracy CE90"
},
"gmd:result": {
"gmd:DQ_QuantitativeResult": {
"gmd:valueUnit": {
"@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
},
"gmd:value": {
"gco:Record": {
"gco:Real": "10"
}
}
}
}
},
{
"gmd:nameOfMeasure": {
"gco:CharacterString": "Absolute vertical accuracy LE90"
},
"gmd:result": {
"gmd:DQ_QuantitativeResult": {
"gmd:valueUnit": {
"@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
},
"gmd:value": {
"gco:Record": {
"gco:Real": "10"
}
}
}
}
}
]
在此示例中我需要检索的对象是最后一个对象(包含字符串“绝对垂直精度 LE90”),但我不能指望它始终处于同一位置。我尝试通过附加来过滤此结果集
[?(@['gmd:nameOfMeasure']['gco:CharacterString']=="Absolute vertical accuracy LE90")]
原始 jsonpath 表达式(生成新表达式
$..['gmd:DQ_AbsoluteExternalPositionalAccuracy'][?(@['gmd:nameOfMeasure']['gco:CharacterString']=="Absolute vertical accuracy LE90")]
但是filter语句对结果没有影响。只是为了好玩,我在 https://jsonpath.herokuapp.com/ 的 Java Jayway jsonpath 实现中尝试了相同的表达式,并成功将结果过滤到一个所需的对象。
谁能告诉我如何使用 jsonpath-plus 正确过滤这个结果集?
使用
查询您的示例 JSON$[?(@['gmd:nameOfMeasure']['gco:CharacterString']==="Absolute vertical accuracy LE90")]
对我有用;也许你可以简单地分两步查询。
由于只提供了内部 JSON,我只能在查询完整 JSON 时猜测问题所在。需要注意的一件事:JsonPath-Plus 期望 JSON 包含在方括号中:
[
{"gmd:DQ_AbsoluteExternalPositionalAccuracy": [
...inner json
}
]