We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi!
Is there any way I can access a key set from an JSONObject (using JSONObject.keys()) giving a specific index? Am I able to convert it to some type a an Array?
Thanks.
Answers
Works perfectly. Thanks!
String[] properties = (String[]) json.keys().toArray(new String[json.size()]);
Can you show me where I can read up more info of what is happening in this line of code? Or explain it? Just want to understand this part: (String[]) json.keys().toArray(new String[json.size()]); I understand that it builds a new String array with the same size as JSONObject, just don't get the (String[])
Thanks!
(String[]) json.keys().toArray(new String[json.size()]);
Let's do it by parts, shall we?
String[]
from that via its toArray() method.new
array instance w/ at least the same size and type of Set<String>.size().String[]
rather than anObject[]
type, we use its(cast)
operator.Object[]
toString[]
via(String[])
typecasting operator.Thanks for such detailed description. In response to your first comment I was going to say you were doing a (cast), but I was afraid to say something stupid. And this is the only part that I don’t really get. I've seen this a few times when working with arrays of objects but not yet realized in full what it does. But that's my fault, I have to read about it!
Thanks for the help and explanation!