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