我从一个curl
命令得到了这样的JSON
回复:
[
{
"cid": 49,
"pyn": "yi4",
"hans": "亿",
"hant": "億",
"tid": 68,
"l10n": "cent million",
"pid": 1,
"pos": "num",
"pos_txt": ""
},
{
"cid": 50,
"pyn": "yi4",
"hans": "亿",
"hant": "億",
"tid": 69,
"l10n": "100 millions",
"pid": 1,
"pos": "num",
"pos_txt": ""
}
]
如何使用Bash
或命令行(例如underscore
)计算数组中的项数(这里是2
)?
发布于 2014-01-26 03:50:26
发布于 2016-03-31 18:21:46
最短的表达式是
curl 'http://…' | jq length
发布于 2021-08-24 07:10:54
如果正在从文件中读取JSON
,请尝试以下命令-
number_of_objects=`jq '. | length' json_file_name.json`
echo $number_of_objects
如果JSON
数组位于JSON
中的键中,如下所示-
{
"fruits": [
"apples",
"oranges",
"pears"
]
}
试试这个-
number_of_objects=`jq '.fruits | length' json_file_name.json`
echo $number_of_objects
(您必须下载jq
才能使用此解决方案)
https://stackoverflow.com/questions/21334348
复制相似问题