简体   繁体   中英

Remove specific values in a JSONArray

I'm have a RecyclerView with selectable items in it. If an item is selected, I put a new field in its JSONObject("selected": true). Below is my JSONArray from logcat called (filteredList).

This is my 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):

[{
"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" . 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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