简体   繁体   English

无需循环获取JSONArray中的索引位置

[英]Without looping get the index position in a JSONArray

I have the following JSONObject 我有以下JSONObject

{"results":2,"object":[{"id":10,"rank":12},{"id":21,"rank":22}.........{"id":n,"rank":n}]}

Now if I have an id: 21, how do I get the position of that object? 现在,如果我有一个ID:21,如何获取该对象的位置?

Is there any direct way to get the position of that id? 有没有直接方法可以获取该ID的位置?

Like 21 is in 2nd position in that array.So the index position is 1. Is there any direct way without looping the JSONArray to improve the performance? 就像21在该数组中的第二位置。因此,索引位置为1。是否有任何直接方法可以不循环JSONArray来提高性能?

One thing you can do is to re-structure your json like 您可以做的一件事是像

{"results":2,"object":{10:{"pos":1,"rank":12},21:{"pos":2,"rank":22}.........n:{"pos":n,"rank":n}}}

Then you can directly fetch it using object[ur_id].pos 然后,您可以使用object[ur_id].pos直接获取它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM