Java 从 JSONArray 中删除 JSON 对象 - Jettison

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19837532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:36:01  来源:igfitidea点击:

Remove JSON object from JSONArray - Jettison

javajsonjettison

提问by Timothy Rajan

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

有没有一种直接的方法可以使用 index.js 删除存储在 JSONArray 中的 JSONObject?我尝试了所有的可能性。仍然无法从 JSON 数组中删除 JSON 对象。任何提示都会有帮助谢谢

采纳答案by Jhanvi

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

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

回答by Ranjit Swain

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

只需在 json 数组中获取 JSON 对象的索引

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

并通过 array.splice(index,howmany,item1,....,itemX) 方法删除 json 对象

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

有关更多信息,请使用此链接 http://www.w3schools.com/jsref/jsref_splice.asp