英文:
Jayway JSONPath Expession for Both Object and Array
问题
我在尝试检索值时遇到了问题,因为父元素既可以是JSONArray也可以是JSONObject。为此,我使用了Deepcopy语法来检索它。现在的问题是,如果子属性在内部数组中存在,我会得到额外的值。
例如:
JpathExpression:
$.store..book..innerBook..category
结果是:
[
"innerReference1",
"innerBook1Ref1",
"innerReference2"
]
示例1的期望结果是:
[
"innerReference1",
"innerReference2"
]
示例1:
{
"store": {
"book": [
{
"innerBook": [
{
"category": "innerReference1",
"author": "Nigel Rees",
"innerBook1": [
{
"category": "innerBook1Ref1"
}
]
},
{
"category": "innerReference2",
"author": "Nigel Rees"
}
]
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
示例2:
{
"store": {
"book": [
{
"innerBook":
{
"category": "innerReference1",
"author": "Nigel Rees",
"innerBook1": [
{
"category": "innerBook1Ref1"
}
]
}
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
示例2的期望结果是:
[
"innerReference1"
]
英文:
I am having to trouble to retrieve value if parent element can be JSONArray as well as JSONObject. For that I used Deepcopy syntax to retrieve it. Now Problem is am getting additional values if child attribute exists in Inner Array also.
Eg:
JpathExpression:
$.store..book..innerBook..category
Result is :
[
"innerReference1",
"innerBook1Ref1",
"innerReference2"
]
Example 1 Expected Result is :
[
"innerReference1",
"innerReference2"
]
Example 1:
{
"store": {
"book": [
{
"innerBook": [
{
"category": "innerReference1",
"author": "Nigel Rees",
"innerBook1": [
{
"category": "innerBook1Ref1"
}
]
},
{
"category": "innerReference2",
"author": "Nigel Rees"
}
]
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
Example 2:
{
"store": {
"book": [
{
"innerBook":
{
"category": "innerReference1",
"author": "Nigel Rees",
"innerBook1": [
{
"category": "innerBook1Ref1"
}
]
}
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
Example 2 Expected Result is :
[
"innerReference1"
]
答案1
得分: 1
A little late, however, we do not need to use recursive descent ..
after innerBook
to walk the items in the array; with Jayway's we can use innerBook[*].category
to retrieve the desired result for example 1:
$.store..book..innerBook[*].category
Example 2 requires to remove the array accessor:
$.store..book..innerBook.category
You cannot have innerBook
as array and object in one JSON since it wouldn't be valid.
If you have an API (or so) that serves JSON with an innerbook
property of array or object type (which would be a badly designed API since the whole point of an API is to have expected responses) you could either query both paths or make the access of categories depending on the existence of innerBook1
:
$.store.book..innerBook[?(@.innerBook1)]..category
Which only returns for both examples
[
"innerReference1",
"innerBook1Ref1"
]
Try it online here.
英文:
A little late, however, we do not need to use recursive descent ..
after innerBook
to walk the items in the array; with Jayway's we can use innerBook[*].category
to retrieve the desired result for example 1:
$.store..book..innerBook[*].category
Example 2 requires to remove the array accessor:
$.store..book..innerBook.category
You cannot have innerBook
as array and object in one JSON since it wouldn't be valid.
If you have an API (or so) that serves JSON with an innerbook
property of array or object type (which would be a badly designed API since the whole point of an API is to have expected responses) you could either query both paths or make the access of categories depending on the existence of innerBook1
:
$.store.book..innerBook[?(@.innerBook1)]..category
Which only returns for both examples
[
"innerReference1",
"innerBook1Ref1"
]
Try it online here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论