gpt4 book ai didi

jsonpath - 如何使用jsonpath表达式在数组列表中查找最后一个元素?

转载 作者:行者123 更新时间:2023-12-03 13:43:10 50 4
gpt4 key购买 nike

我有如下的json字符串

[
{
"topic": "inputTopic",
"key": "0",
"message": "test",
"partition": 0,
"offset": 0
},
{
"topic": "inputTopic",
"key": "0",
"message": "test",
"partition": 0,
"offset": 1
},
{
"topic": "inputTopic",
"key": "0",
"message": "test",
"partition": 0,
"offset": 2
},
{
"topic": "inputTopic",
"key": "0",
"message": "test",
"partition": 0,
"offset": 3
},
{
"topic": "inputTopic",
"key": "0",
"message": "test",
"partition": 0,
"offset": 4
},
{
"topic": "inputTopic",
"key": "0",
"message": "test",
"partition": 0,
"offset": 5
},
{
"topic": "inputTopic",
"key": "0",
"message": "test",
"partition": 0,
"offset": 6
},
{
"topic": "inputTopic",
"key": "0",
"message": "Hi Test",
"partition": 0,
"offset": 7
},
{
"topic": "inputTopic",
"key": "0",
"message": "Hi Test",
"partition": 0,
"offset": 8
},
{
"topic": "inputTopic",
"key": "0",
"message": "Hi Test",
"partition": 0,
"offset": 9
}
]

如何使用jsonpath表达式获取最后一个项目?
 {
"topic": "inputTopic",
"key": "0",
"message": "Hi Test",
"partition": 0,
"offset": 9
}

最佳答案

正如您可以在docs中阅读的内容一样,您可以ether做一个

$..book[(@.length-1)]

正如邓肯在上文中提到的。或者你可以
$..book[-1:]

我个人认为前者更具表现力,后者则更聪明。后者似乎也是预期的默认值。我会说这是个人风格的问题。

关于jsonpath - 如何使用jsonpath表达式在数组列表中查找最后一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32153985/

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