您可以使用remove()方法从JSONArray 对象中删除元素。此方法接受一个整数并删除该特定索引中的元素。
import org.json.JSONArray; public class RemoveFromJsonArray { public static void main(String args[]) throws Exception { String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"}; JSONArray jsArray = new JSONArray(); for (int i=0; i < myArray.length; i++) { jsArray.put(myArray[i]); } System.out.println(jsArray); jsArray.remove(3); System.out.println("After deleting ::"+jsArray); } }
输出结果
["JavaFX","HBase","JOGL","WebGL"] After deleting ::["JavaFX","HBase","JOGL"]