从JSONArray删除JSON对象 - 抛弃 [英] Remove JSON object from JSONArray - Jettison

查看:612
本文介绍了从JSONArray删除JSON对象 - 抛弃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种直接的方式使用索引来删除存储在JSONArray的JSONObject的。我尝试了所有的可能性。仍然无法从JSON数组中删除JSON对象。任何暗示会有所帮助
谢谢

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

推荐答案

在Java的JSON,有删除的JSONObject没有直接的方法,但使用的 JSON-简单,这是简单的这样做的:

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);

这篇关于从JSONArray删除JSON对象 - 抛弃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆