Extracting Keys from a JSONObject using keySet()

The javadoc says:

public interface JsonObject
extends JsonStructure, Map<String,JsonValue>

So, a JSONObject is a Map whose keys are of type String, and whose values are of type JSONValue.

And the javadoc of Map<K, V>.keySet() says:

Set<K> keySet()

Returns a Set view of the keys contained in this map

So, what JSONObject.keySet() returns is a Set<String> (which is quite logical, since keys of JSON objects are strings).

So all that you want is:

Set<String> keys = posts.keySet();

Leave a Comment