简体   繁体   English

删除JSONArray中的特定值

[英]Remove specific values in a JSONArray

I'm have a RecyclerView with selectable items in it. 我有一个带有可选项目的RecyclerView。 If an item is selected, I put a new field in its JSONObject("selected": true). 如果选择了一项,则将新字段放入其JSONObject(“ selected”:true)中。 Below is my JSONArray from logcat called (filteredList). 以下是我从logcat调用(filteredList)的JSONArray。

This is my filteredList JSON : 这是我的filteredList JSON:

[{
"nr": 1,
"grpnr": 0,
"bezeich": "MORE SALT",
"selected": true
}, {
"nr": 2,
"grpnr": 0,
"bezeich": "MORE SWEET"
}, {
"nr": 3,
"grpnr": 0,
"bezeich": "MORE PEPPER"
}, {
"nr": 4,
"grpnr": 0,
"bezeich": "MORE CHILLI",
"selected": true
}, {
"nr": 5,
"grpnr": 0,
"bezeich": "COLD"
}, {
"nr": 6,
"grpnr": 0,
"bezeich": "HOT"
}, {
"nr": 7,
"grpnr": 0,
"bezeich": "SMALL"
}, {
"nr": 8,
"grpnr": 0,
"bezeich": "LARGE"
}, {
"nr": 9,
"grpnr": 0,
"bezeich": "MEDIUM COOKED"
}, {
"nr": 10,
"grpnr": 0,
"bezeich": "WELL DONE"
}]

I'd like to make my filteredList JSON look like this (essentially removing all selected fields): 我想使我的filteredList JSON看起来像这样(基本上删除所有selected字段):

[{
"nr": 1,
"grpnr": 0,
"bezeich": "MORE SALT"
}, {
"nr": 2,
"grpnr": 0,
"bezeich": "MORE SWEET"
}, {
"nr": 3,
"grpnr": 0,
"bezeich": "MORE PEPPER"
}, {
"nr": 4,
"grpnr": 0,
"bezeich": "MORE CHILLI"
}, {
"nr": 5,
"grpnr": 0,
"bezeich": "COLD"
}, {
"nr": 6,
"grpnr": 0,
"bezeich": "HOT"
}, {
"nr": 7,
"grpnr": 0,
"bezeich": "SMALL"
}, {
"nr": 8,
"grpnr": 0,
"bezeich": "LARGE"
}, {
"nr": 9,
"grpnr": 0,
"bezeich": "MEDIUM COOKED"
}, {
"nr": 10,
"grpnr": 0,
"bezeich": "WELL DONE"
}]

As we see, some object have the field "selected" . 如我们所见,某些对象具有字段"selected" I want to know how to delete only this field. 我想知道如何仅删除此字段。 I tried this and this but its not working in my case. 我想这个这个 ,但它不是我的情况下工作。 Any answer will help, thank in advance! 任何答案都会有所帮助,在此先感谢!

It doesn't work because my CheckBox only removes the last checked item. 它不起作用,因为我的CheckBox仅删除了最后一个选中的项目。

here's my code snippet: 这是我的代码段:

JSONObject check = new JSONObject();
    try {
        check = orderList.getJSONObject(intCurrArticleNr);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if(check.has("spesial-request")) {
        try {
            String arrayRemarkString = check.getString("spesial-request");

            if (remarkObj.toString().equalsIgnoreCase(arrayRemarkString)) {
                currRemark = new JSONArray(check.getString("spesial-request"));
                edt_reqList.setText(currRemark.toString().trim());

                System.out.println(currRemark);
            }
            else {
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    else {

// here i have a probelem //
        remarkObj = new JSONArray();
//            objectReqList.remove("selected");

        int len = filteredReqList.length();
        JSONArray teslist = new JSONArray();
        if (filteredReqList == null) {
            for (int i=0;i<len;i++){
                filteredReqList.remove(i);
            }
        }
    }

You can just iterate and remove, try with below - It works fine. 您可以重复进行删除,请尝试以下操作-效果很好。

JSONParser parser = new JSONParser();

Object obj = parser.parse(new FileReader(
        "sample.json"));

JSONArray jsonArray = (JSONArray) obj;
for (int i = 0; i < jsonArray.size(); i++) {
    System.out.println("Before --" + jsonArray.get(i).toString());
    JSONObject jsonObject = (JSONObject)jsonArray.get(i);
    jsonObject.remove("selected");
    System.out.println("After --" + jsonArray.get(i).toString());
}
System.out.println("Final Output --" + jsonArray.toString());

Output - 输出-

Before --{"selected":true,"nr":1,"grpnr":0,"bezeich":"MORE SALT"}
After --{"nr":1,"grpnr":0,"bezeich":"MORE SALT"} 
Before --{"nr":2,"grpnr":0,"bezeich":"MORE SWEET"} 
After --{"nr":2,"grpnr":0,"bezeich":"MORE SWEET"} 
Before --{"nr":3,"grpnr":0,"bezeich":"MORE PEPPER"} 
After --{"nr":3,"grpnr":0,"bezeich":"MORE PEPPER"} 
Before --{"selected":true,"nr":4,"grpnr":0,"bezeich":"MORE CHILLI"} 
After --{"nr":4,"grpnr":0,"bezeich":"MORE CHILLI"} 
Before --{"nr":5,"grpnr":0,"bezeich":"COLD"} 
After --{"nr":5,"grpnr":0,"bezeich":"COLD"} 
Before --{"nr":6,"grpnr":0,"bezeich":"HOT"} 
After --{"nr":6,"grpnr":0,"bezeich":"HOT"} 
Before --{"nr":7,"grpnr":0,"bezeich":"SMALL"} 
After --{"nr":7,"grpnr":0,"bezeich":"SMALL"} 
Before --{"nr":8,"grpnr":0,"bezeich":"LARGE"} 
After --{"nr":8,"grpnr":0,"bezeich":"LARGE"} 
Before --{"nr":9,"grpnr":0,"bezeich":"MEDIUM COOKED"} 
After --{"nr":9,"grpnr":0,"bezeich":"MEDIUM COOKED"} 
Before --{"nr":10,"grpnr":0,"bezeich":"WELL DONE"} 
After --{"nr":10,"grpnr":0,"bezeich":"WELL DONE"} 
Final Output -- [{
    "nr": 1,
    "grpnr": 0,
    "bezeich": "MORE SALT"
}, {
    "nr": 2,
    "grpnr": 0,
    "bezeich": "MORE SWEET"
}, {
    "nr": 3,
    "grpnr": 0,
    "bezeich": "MORE PEPPER"
}, {
    "nr": 4,
    "grpnr": 0,
    "bezeich": "MORE CHILLI"
}, {
    "nr": 5,
    "grpnr": 0,
    "bezeich": "COLD"
}, {
    "nr": 6,
    "grpnr": 0,
    "bezeich": "HOT"
}, {
    "nr": 7,
    "grpnr": 0,
    "bezeich": "SMALL"
}, {
    "nr": 8,
    "grpnr": 0,
    "bezeich": "LARGE"
}, {
    "nr": 9,
    "grpnr": 0,
    "bezeich": "MEDIUM COOKED"
}, {
    "nr": 10,
    "grpnr": 0,
    "bezeich": "WELL DONE"
}]

This should do the trick: 这应该可以解决问题:

for (int i = 0; i < filteredReqList.length(); i++) {
    JSONObject item = filteredReqList.getJSONObject(i);
    item.remove("selected");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM