问题描述
我想基于嵌套JSON响应中的条件提取元素。使用下面广泛搜索的示例,我想提取“ Foo Bar”创作的所有书籍的书店号:
{
"store": {
"storeId": 1
"book": [
{
"category": "reference","author": [
{
"Nigel Rees","Foo Bar"
}
],"title": "Sayings of the Century","price": 8.95
},{
"category": "fiction","author": [
{
"Evelyn Waugh"
}
],"title": "Sword of Honour","price": 12.99
}
],"bicycle": {
"color": "red","price": 19.95
}
},"expensive": 10
}
我尝试使用类似以下的表达式: $ .store [?(@ .. author =='Foo Bar')]。storeId
我还尝试使用以下方法查看是否有任何商店退回: $ .store [*]。book [?(@。author =='Foo Bar')]
但未返回任何结果...请帮助。谢谢!
解决方法
您产生了is not a valid JSON,因此您将无法在其上使用JSON Extractor。
如果您修改this good example,甚至无法正确复制并粘贴以包含storeId
:
{
"store": {
"storeId": 1,"book": [
{
"category": "reference","author": "Nigel Rees","title": "Sayings of the Century","price": 8.95
},{
"category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","price": 12.99
},"author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99
},"author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99
}
],"bicycle": {
"color": "red","price": 19.95
}
},"expensive": 10
}
您将可以使用以下表达式提取storeId
的作者之一Nigel Rees
:
$.store[?(@.book[?(@.author=="Nigel Rees")])].storeId
演示:
更多信息: