To get all the keys in JSONObject into String array

后端 未结 2 704
谎友^
谎友^ 2020-12-11 17:40

I want to create a json object from existing json object. For this i want to get all the keys in JSONObject to a String[] array. Is there any default method to get the keys

相关标签:
2条回答
  • 2020-12-11 18:10

    There is not getNames(), but there is Names()

    0 讨论(0)
  • 2020-12-11 18:12

    To construct JSONObject from other JSONObject you can use constructor that accept JSONObject and array of keys names that should be copied. To do it:

    Iterator keysToCopyIterator = firstJSONObject.keys();
    List<String> keysList = new ArrayList<String>();
    while(keysToCopyIterator.hasNext()) {
        String key = (String) keysToCopyIterator.next();
        keysList.add(key);
    }
    String[] kesyArray = keysList.toArray(new String[keysList.size()]);
    JSONObject secondJSONObject = new JSONObject(firstJSONObject, );
    
    0 讨论(0)
提交回复
热议问题