use*_*850 8 java json jsonpath
我有一个JSON这样的文件:
{
"objects": [{
"type": "FirstType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheFirstType",
"font": "18px arial"
},
"id": "18e"
},
(...)
{
"type": "SecondType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheSecondType",
"font": "18px arial"
},
"id": "18f"
}
],
"background": "#ffffff"
}
Run Code Online (Sandbox Code Playgroud)
我的目标是删除 Fe中某个type和 的节点,如果我想删除命名和的元素。我会得到:iddetailstypeFirstTypeid1
{
"objects": [
(...)
{
"type": "SecondType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheSecondType",
"font": "18px arial"
},
"id": "18f"
}
],
"background": "#ffffff"
}
Run Code Online (Sandbox Code Playgroud)
我想我部分实现了这一点:
final DocumentContext jsonContext = JsonPath.parse(element.getJsonContent());
jsonContext.delete("$['objects'][?(@.type == 'FirstType')][?(@.details.id == '1')]");
Run Code Online (Sandbox Code Playgroud)
但我也想考虑typeand idin details,但我不确定两个过滤表达式是否写得正确。我感觉我被困在这里了
编辑:已解决
好的。为了将来的参考,正确的形式如下:
DocumentContext jsonContext = JsonPath.parse(element.getJsonContent());
jsonContext.delete("$['objects'][?(@.type == 'FirstType' && @.details.id == 1)]");
element.setJsonContent(jsonContext.jsonString());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10213 次 |
| 最近记录: |