1

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);
            }
        }
    }
Community
  • 1
  • 1
MrX
  • 953
  • 2
  • 16
  • 42
  • What do you mean it is not working? Does it throw an exception? Does it just not remove the fields? Please elaborate. Also show us the expected json vs the json u get. Lastly some code would be nice so we can see what you are doing wrong – nbokmans Feb 08 '17 at 08:30
  • Sorry i'll edit – MrX Feb 08 '17 at 08:31
  • while filling your Recycler view check if `selected` exists then don't put it in recycler view . its one approach other may also be there. – jack jay Feb 08 '17 at 08:36
  • @jackjay i'll try that , but i'm still curious with this case – MrX Feb 08 '17 at 08:45
  • 1
    @MarioMargoPradipta You can just iterate and remove, check my answer below - I tested with your JSON, it gives me the expected result – Srikanth Balaji Feb 08 '17 at 09:04

2 Answers2

1

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"
}]
Srikanth Balaji
  • 2,638
  • 4
  • 18
  • 31
1

This should do the trick:

for (int i = 0; i < filteredReqList.length(); i++) {
    JSONObject item = filteredReqList.getJSONObject(i);
    item.remove("selected");
}
marmor
  • 27,641
  • 11
  • 107
  • 150