Remove JSON object from JSONArray - Jettison

ぐ巨炮叔叔 提交于 2019-12-05 14:24:54

In java-json , there is no direct method to remove jsonObject, but using json-simple , it is simple to do so:

        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);

just get the index of the JSON object in the json array

and remove the json object by array.splice(index,howmany,item1,.....,itemX) method

for more information just use this link http://www.w3schools.com/jsref/jsref_splice.asp

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