4

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

4

3 回答 3

6

在 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-08T07:51:43.890 回答
0

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

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

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

于 2013-11-07T14:04:11.610 回答
0

您是否尝试过使用删除来执行此操作?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

于 2013-11-07T13:51:22.490 回答