Extracting Keys from a JSONObject using keySet()

前端 未结 5 1621
走了就别回头了
走了就别回头了 2020-12-03 22:27

I\'m trying to extract the keys from a JSON Object. The JSON object, in this case, is obtained by making an API call to a social networking site called SkyRock and

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 23:14

    The javadoc says:

    public interface JsonObject
    extends JsonStructure, Map
    

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

    And the javadoc of Map.keySet() says:

    Set keySet()
    
    Returns a Set view of the keys contained in this map
    

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

    So all that you want is:

    Set keys = posts.keySet();
    

提交回复
热议问题