我如何得到seats对象中json数组的最后一个元素。我想得到的countryid的值为845,但是这个json是动态的,所以我想得到seats对象中的最后一个元素。我的API是这样构造的。先谢谢你。
{
"expirationDate":"April 21, 2017",
"remainingDays":325,
"seats":[{"activeStatus":"S","pid":"TE70","firstName":"TE70","countryid":840},
{"activeStatus":"Y","pid":"TE80","firstName":"TE80","countryid":845}]
}
5条答案
按热度按时间sqougxex1#
您可以通过按索引访问
jsonData.seats
数组来实现这一点,最后一项的索引等于jsonData.seats.length-1
简单地说:
ehxuflar2#
试试这个:
ds97pgxw3#
注:@ www.example.com 代码
使用DOT在
example.at(-1)
i5desfxk4#
你可以反转数组,然后得到第一个元素:
el.reverse()[0]
例如:
klsxnrf15#
对于任何希望在JSONPath语法中实现这一点的人,可以使用
$.seats[-1:]
实现。如果你想做一些事情,比如获取嵌套在最后一个元素中的countryid
属性,它应该是$.seats[-1:][countryid]
。这对于在API客户端中测试端点时链接请求很方便。