从JSONArray中删除JSON对象 - Jettison

Tim*_*jan 4 java json jettison

是否有通过使用索引删除存储在JSONArray中的JSONObject的直接方法.我尝试了所有的可能性.仍然无法从JSON数组中删除JSON对象.任何提示都会有所帮助谢谢

Jha*_*nvi 6

在java-json中,没有直接的方法来删除jsonObject,但是使用json-simple,这样做很简单:

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        jsonObject.put("key1", "value1");
        jsonObject1.put("key2", "value2");
        jsonObject2.put("key3", "value3");
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);

        //........ Whole Json Array
        System.out.println(jsonArray);


        //To remove 2nd jsonObject (index starts from 0)

        jsonArray.remove(1);


        // Now the array will not have 2nd Object
        System.out.println(jsonArray);
Run Code Online (Sandbox Code Playgroud)

  • 此方法至少需要API级别19. (6认同)