How can I access a JSONObject.keys() set?

edited May 2014 in How To...

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.

Tagged:

Answers

  • // forum.processing.org/two/discussion/5344/
    // how-can-i-access-a-jsonobject-keys-set
    
    final JSONObject json = new JSONObject();
    String[] properties;
    
    void setup() {
      json.setInt("id", 0);
      json.setString("species", "Panthera leo");
      json.setString("name", "Lion");
    
      println(json);
      println();
    
      properties = (String[]) json.keys()
        .toArray(new String[json.size()]);
    
      println(properties);
      exit();
    }
    
  • edited May 2014

    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!

  • edited May 2014 Answer ✓

    (String[]) json.keys().toArray(new String[json.size()]);

    Let's do it by parts, shall we?

    • Variable json keeps a reference to a JSONObject instance.
    • Then the undocumented method keys() is called upon that reference.
    • Which in turn invokes method keySet() from its internal HashMap<String, Object> field.
    • That returns all the HashMap's keys represented as a Set instance.
    • Now as a Set data structure we could choose its iterator() method to get us an Iterator in order to use it in a loop block.
    • Instead, I've decided to get a String[] from that via its toArray() method.
    • For that, we need a new array instance w/ at least the same size and type of Set<String>.size().
    • Next we use that array as the argument for toArray(). All of the key elements in the Set are copied into it.
    • Problem is that they are still considered an ordinary Object rather than its true type String.
    • Perhaps b/c JSONObject's keys() method returns a generic Set rather than a specific Set<String> type.
    • To tell Java we've got a String[] rather than an Object[] type, we use its (cast) operator.
    • In this case, we upcast the toArray()'s returned Object[] to String[] via (String[]) typecasting operator.
    • So that reference can be validly assigned to a String variable.
  • edited May 2014

    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!

Sign In or Register to comment.