有没有一种直接的方法可以使用索引删除存储在JSONArray中的JSONObject。我试过了所有的可能性。仍然无法从JSON数组中删除JSON对象。任何提示都会有帮助的,谢谢
发布于 2013-11-08 15:51:44
在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);发布于 2013-11-07 21:51:22
您是否尝试过使用delete来执行此操作?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
发布于 2013-11-07 22:04:12
只需在JSON数组中获取json对象的索引
并通过array.splice(索引,howmany,item1,.....,itemX)方法移除json对象
有关更多信息,请使用此链接http://www.w3schools.com/jsref/jsref_splice.asp
https://stackoverflow.com/questions/19837532
复制相似问题