假设我有这个流行的数据样本。
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"color": "red",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"color": "blue",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"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
}
我想要的是book
所有category == fiction
以及所有bicycle
color == red
。
就是说,我想要,
{
"category": "fiction",
"author": "Evelyn Waugh",
"color": "red",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"color": "blue",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"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
}
我知道,我可以使用$.store.book[?(@.category == "fiction")]
来实现目标书籍,$.store.bicycle[?(@.color == 'red')]
来实现目标自行车。
但我怎样才能一次性实现这两个目标?
答案 0 :(得分:0)
您可以使用&&
和||
运营商。
$.store.*[?(@.color== 'red' || @.category == 'fiction')]
将根据需要获取结果
答案 1 :(得分:0)
我建议您使用JSR223 PostProcessor和Groovy language来实施您的方案。从过滤后的数据中提取元素和构建新JSON的示例代码如下所示:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def json = new JsonSlurper().parseText(prev.getResponseDataAsString())
def fictionBooks = json.store.book.findAll {it.category == "fiction"}
def redBikes = json.store.bicycle.findAll{it.color="red"}
JsonBuilder builder = new JsonBuilder(fictionBooks + redBikes)
prev.setResponseData(builder.toPrettyString())
以上代码将使用筛选的JSON
替换父采样器响应数据参考文献:
答案 2 :(得分:0)
要退还书籍和自行车,您需要:
ValidateLogin(e)
{
if(this.state.formValid)
{
let userid = this.state.userId;
let password = this.state.password;
let url = "http://localhost:3727/api/Login/ValidateUser";
fetch(url, {
method: "POST",
mode: 'no-cors',
body : "userid=" + userid +"&password=" + password
}).then((response) => console.Log(respose));
}
};
对于颜色为==红色或类别==小说的书籍,您需要:
$..*[?(@.color== 'red' || @.category == 'fiction')]