从JSON对象中删除JSON对象的最佳方法

时间:2014-10-29 15:57:10

标签: android json jsonobject

假设我有一个JSONObject

{
    "poll_answers":{
        "213":{
            "poll_answer_text":"Black",
            "poll_answer_id":"213"
        },
        "214":{
            "poll_answer_text":"White",
            "poll_answer_id":"214"
        },
        "218":{
            "poll_answer_text":"Colorful",
            "poll_answer_id":"218"
        }
    }
}

使用键“214”删除** JSONObject的最相关/最佳方法是什么(例如),因为android remove 方法返回先前由键映射但未给定的值对象除了我要删除的对象。所以解决方案应该如下:

{
    "poll_answers":{
        "213":{
            "poll_answer_text":"Black",
            "poll_answer_id":"213"
        },
        "218":{
            "poll_answer_text":"Colorful",
            "poll_answer_id":"218"
        }
    }
}

1 个答案:

答案 0 :(得分:1)

remove()方法就是您想要的 - 但是,您必须更深层次地导航,因为它包含在另一个JSONObject中:

JSONObject object = new JSONObject("your json string");
object.getJSONObject("poll_answers").remove("214");
相关问题