我下面有这样的杰森
{
'orderList': [
{
'productId': 'AAA',
},
{
'productId': 'BBB',
}
]
}
当orderList
中的所有项目都具有非空的productId
属性时,JSON字符串有效。我正尝试使用JsonPath进行验证,现在我遇到了问题。 $.orderList[*]['productId']
可以将orderList
中的所有productId提取为类似于['AAA', 'BBB']
的String数组,这使我可以检查其所有项目都是非空的,但是当productId
道具不存在像下面一样
{
'orderList': [
{
'productId': 'AAA',
},
{
}
]
}
JSON路径
$.orderList[*]['productId']
将数组作为['AAA']
(不是['AAA', null]
或['AAA', '']
),因此它可以通过我的检查函数。该怎么做对呢?
答案 0 :(得分:1)
这就是jsonpath的工作方式。如果未在对象中找到该属性,它将在结果数组中忽略该属性,而不是添加null
或undefined
值。因此,如果要在结果数组中获取null
,则需要以null
作为值添加该属性。
{
"orderList": [
{
"productId": "AAA"
},
{
"productId": null
}
]
}
结果将
[
"AAA",
null
]