Generify JSONObject string keys

痞子三分冷 提交于 2019-12-02 13:15:24

The answer you provided a link to is using a different class than the one you are using. If you look at the source for org.json.JSONObject you'll find the following:

public Iterator<String> keys() {
    return this.keySet().iterator();
}

Which means you can write the following code:

    JSONObject obj = new JSONObject();
    obj.put("key1", "value1");
    obj.put("key2", "value2");
    Iterator<String> keys = obj.keys();

    while(keys.hasNext()){
        System.out.println(keys.next());
    }

and it will generate the following output:

key1
key2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!