【发布时间】:2015-03-05 09:31:22
【问题描述】:
我从 REST 服务调用中获得 JSON 响应,并且只想从响应中选择一些字段。我正在使用 JSONPath 过滤掉这些字段。下面是 JSON 示例:
{
"store": {
"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
},
{
"category": "fiction",
"author": "Herman Melville",
"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
}
}
}
例如我想从类别为“参考”的响应中选择作者和标题。我正在使用下面的 JSONPath
$.store.book[?(@.category='reference')]
这给了我以下回应:
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}
但是,我不想要所有字段。我只想要作者和标题。如果我尝试$.store.book[?(@.category='reference')]['author'],它会给我作者姓名,但如果我尝试$.store.book[?(@.category='reference')]['author', 'title'],它不会返回任何内容。
JSONPath 中是否有任何规定可以选择(或排除)有条件或无条件的字段?
我正在使用http://jsonpath.curiousconcept.com/ 来测试 JSONPath。
提前致谢。
【问题讨论】: