Jayway JSONPath 表达式适用于对象和数组。

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月9日 01:45:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63318501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定