如何过滤数组并获取过滤后的长度?
How to filter an array and get the filtered length?
以典型的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
}
}
}
我要过滤图书,获取过滤结果数组大小
我将 json 路径写为:$..book[?(@.category in ['fiction'])].size()
预期值为 3。
但实际上我得到了14.
试过 $..book[?(@.category in ['fiction'])].length()
仍然得到相同的结果:14
你为什么不用这样的东西:
int count = 0;
for (Book book : store) {
count = book.category.equals('fiction')? count+1 : count;
}
您需要使用:
$..book[?(@.category=="fiction")]
很多例子在:
https://www.npmjs.com/package/jsonpath
完整的js代码:
'use strict';
const $ = require( "jquery" );
const fs = require('fs');
var jp = require('jsonpath');
var store = { "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
}
}
};
var book = store["store"]
var len = jp.query(book, '$..book[?(@.category=="fiction")]').length
console.debug(len)
$.store.book[?(@.category=="fiction")].length()
//Output
[
4,
5,
5
]
将给出每本书中的属性数。这就是 $..book[?(@.category == "fiction")].length()
得到 14 (4+5+5) 的原因
现在,如果您对生成的数组使用长度函数,您将得到 3,这是所需的结果。
$.length($.store.book[?(@.category == "fiction")].length())
在这里试试:Jayway JsonPath Evaluator
这是满足您要求的 work-around,github length() not working as expected
中有许多类似问题的未解决问题
以典型的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
}
}
}
我要过滤图书,获取过滤结果数组大小
我将 json 路径写为:$..book[?(@.category in ['fiction'])].size()
预期值为 3。 但实际上我得到了14.
试过 $..book[?(@.category in ['fiction'])].length()
仍然得到相同的结果:14
你为什么不用这样的东西:
int count = 0;
for (Book book : store) {
count = book.category.equals('fiction')? count+1 : count;
}
您需要使用:
$..book[?(@.category=="fiction")]
很多例子在: https://www.npmjs.com/package/jsonpath
完整的js代码:
'use strict';
const $ = require( "jquery" );
const fs = require('fs');
var jp = require('jsonpath');
var store = { "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
}
}
};
var book = store["store"]
var len = jp.query(book, '$..book[?(@.category=="fiction")]').length
console.debug(len)
$.store.book[?(@.category=="fiction")].length()
//Output
[
4,
5,
5
]
将给出每本书中的属性数。这就是 $..book[?(@.category == "fiction")].length()
现在,如果您对生成的数组使用长度函数,您将得到 3,这是所需的结果。
$.length($.store.book[?(@.category == "fiction")].length())
在这里试试:Jayway JsonPath Evaluator
这是满足您要求的 work-around,github length() not working as expected
中有许多类似问题的未解决问题