gpt4 book ai didi

json - 如何使用 JSONPath 获取 JSON 数组的长度?

转载 作者:行者123 更新时间:2023-12-05 04:04:54 24 4
gpt4 key购买 nike

如果我有这样的 JSON 文档

[
{
"number" : "650-462-9154",
"type" : "main"
},
{
"number" : "650-462-1252",
"type" : "fax"
}
]

我可以使用什么 JSONPath 来获取数组长度(即 2),而无需对任何属性值进行硬编码?

使用我拥有的工具,这里是他们提供的一些示例,这并不能帮助我弄清楚我需要什么值。

[
{
"type": "add",
"id": "tt0484562",
"version": 1,
"lang": "en",
"fields": {
"title": "The Seeker: The Dark Is Rising",
"director": "Cunningham, David L.",
"genre": ["Adventure","Drama","Fantasy","Thriller"],
"actor": ["McShane, Ian","Eccleston, Christopher","Conroy, Frances",
"Crewson, Wendy","Ludwig, Alexander","Cosmo, James",
"Warner, Amelia","Hickey, John Benjamin","Piddock, Jim",
"Lockhart, Emma"]
}
},
{
"type": "delete",
"id": "tt0484575",
"link_ref": null,
"version": 2
}
]

$.[0].genre ---> 0
$.[0].fields.genre ---> 1
$.[0].fields.genre[*] ---> 4
$.[*].type ---> 2
$.[1].link_ref ---> 1

最佳答案

您可以使用 .length() 函数从第一个示例中获取数组的长度,如下所示:

$.length

那应该返回这样的东西:

[
2
]

对于第二个示例,您可以使用常规点符号 ($.node1.node2.node3) 访问任何数组键并调用 length() 函数您希望获得其长度的节点 key 。例如,如果你想获得 actor 数组中值的数量,你可以这样做:

$..actor.length

这将返回这样的东西:

[
10
]

测试于 https://codebeautify.org/jsonpath-testerhttp://jsonpath.com/ .

您可以在 jsonpath github repo 中找到更多功能.

关于json - 如何使用 JSONPath 获取 JSON 数组的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51934322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com