简体   繁体   English

Object 和数组的 Jayway JSONPath Expession

[英]Jayway JSONPath Expession for Both Object and Array

I am having to trouble to retrieve value if parent element can be JSONArray as well as JSONObject.如果父元素既可以是 JSONArray 也可以是 JSONObject,我就不得不麻烦地检索值。 For that I used Deepcopy syntax to retrieve it.为此,我使用 Deepcopy 语法来检索它。 Now Problem is am getting additional values if child attribute exists in Inner Array also.如果内部数组中也存在子属性,现在的问题是获得额外的值。

Eg: JpathExpression:例如:JpathExpression:

$.store..book..innerBook..category

Result is:结果是:

[
   "innerReference1",
   "innerBook1Ref1",
   "innerReference2"
]

Example 1 Expected Result is:示例 1 预期结果是:

[
   "innerReference1",
   "innerReference2"
]

Example 1:示例 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:示例 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:示例 2 预期结果是:

[
   "innerReference1"
]

A little late, however, we do not need to use recursive descent .. after innerBook to walk the items in the array;然而,有点晚了,我们不需要在innerBook之后使用递归下降..来遍历数组中的项目; with Jayway's we can use innerBook[*].category to retrieve the desired result for example 1:使用 Jayway,我们可以使用innerBook[*].category来检索所需的结果,例如 1:

$.store..book..innerBook[*].category

Example 2 requires to remove the array accessor:示例 2 需要删除数组访问器:

$.store..book..innerBook.category 

You cannot have innerBook as array and object in one JSON since it wouldn't be valid.您不能将innerBook作为数组和 object 放在一个 JSON 中,因为它无效。

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 : 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类别的访问取决于innerBook1的存在:

$.store.book..innerBook[?(@.innerBook1)]..category

Which only returns for both examples仅返回两个示例

[
   "innerReference1",
   "innerBook1Ref1"
]

Try it online here .在这里在线尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM