Remove JSON object from JSONArray - Jettison

帅比萌擦擦* 提交于 2019-12-07 10:45:10

问题


Is there a direct way to remove an JSONObject stored in the JSONArray by using index. I tried all the possibilities. Still not able to remove the JSON object from the JSON Array. Any hint will be helpful Thanks


回答1:


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



回答2:


Have you tried using delete to do this?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete




回答3:


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



来源:https://stackoverflow.com/questions/19837532/remove-json-object-from-jsonarray-jettison

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