有没有一种直接的方式可以通过索引删除存储在JSONArray中的JSONObject。我尝试了所有可能性。仍然无法从JSONArray中删除JSON对象。任何提示都将有帮助。谢谢。
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);
你尝试过使用 delete 来实现这个功能吗?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
只需在JSON数组中获取JSON对象的索引
然后使用array.splice(index,howmany,item1,.....,itemX)方法删除JSON对象
有关更多信息,请使用此链接http://www.w3schools.com/jsref/jsref_splice.asp