如何获取seats对象中json数组的最后一个元素。我想要获取的国家in的值是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}]
}
发布于 2016-05-31 09:39:29
您可以通过索引来访问jsonData.seats
数组,最后一项的索引等于jsonData.seats.length-1
简单地说:
var countryId = jsonData.seats[jsonData.seats.length-1].countryid
发布于 2016-05-31 09:40:50
试试这个:
var jsonObject = {
"expirationDate":"April 21, 2017",
"remainingDays":325,
"seats":[{"activeStatus":"S","pid":"TE70","firstName":"TE70","countryid":840},
{"activeStatus":"Y","pid":"TE80","firstName":"TE80","countryid":845}]
}
var lastElement = jsonObject.seats[jsonObject.seats.length-1].countryid
发布于 2020-06-26 12:19:38
我使用了上面的建议,并将其用于来自GSON lib的循环JsonElement对象。以防有人在找和我一样的东西。
for (JsonElement obj : entries){
//Do your stuff
if (entries.get(entries.size() - 1).equals(obj)){
//Do stuff if your in last position of the jsonarray array
}else{
//Do stuff until you reach the last position
}
}
https://stackoverflow.com/questions/37542093
复制相似问题