0

i want to remove specific user with its uniquid i have parse json_encode($data); from volley response:

this is my response:

[
     {
        "uniqid":"h5faweWtd", 
        "name":"Test_1", 
        "address":"tst", 
        "email":"ru_tst@tst.cc", 
        "mobile":"12345",
        "city":"indd"
},{
    "uniqid":"h5Wtd", 
    "name":"Test_2", 
    "address":"tst", 
    "email":"ru_tst@tst.cc", 
    "mobile":"1235945",
    "city":"indd4"
},{
    "uniqid":"h5Wtd25", 
    "name":"Test_3", 
    "address":"tdsdsst", 
    "email":"rsfu_tst@tsfst.cc", 
    "mobile":"1263345",
    "city":"indd9"
  }
    ]

I want to remove specific user with its uniquid

I'll easily do it in php with the help of this code:

foreach ($UserList as $key => $value) {
   if (in_array('uniqid', $uniqid_To_Remove)) {
    unset($UserList[$key]);
   }
  }
$UserList = json_encode($UserList);

echo $UserList;

but i want to get response and save it to shared preferences when internet is available and when internet is not available: then i want to get response data from shared preferences and remove specific user from its uniqid. how to do it in android java?

I am trying to delete an entire user. the problem is that the object position is not static, it can be anywhere in the array. the only thing that is static that i have is uniqid.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    Even if you're new, you should know Not to Write Like This because It's Very Annoying to Read Text With Random uppercase Letters. Also, don't bold your text unless you want people not to read it. – James Z Aug 12 '20 at 04:17
  • Sorry for that please give me any solution? – new Devloper Aug 12 '20 at 05:18
  • One more thing, don't ask people to give you solutions. If they can / want, they will. – James Z Aug 12 '20 at 08:55

2 Answers2

0

To filter a JsonArray, you could try this:

       try {
            List<JSONObject> filtered = new ArrayList<>();
            JSONArray array = new JSONArray(); //Replace array with Response array

            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                String id = obj.getString("uniqid");
                if (!id.equals("h5Wtd25")) { //Replace with your own filtered rule
                    filtered.add(obj); 
                }
            }
        } catch (JSONException e) {

        }
navylover
  • 12,383
  • 5
  • 28
  • 41
0

Assumed that you have the list of User, you can use filter to get your new list which remove the user with specific id

private List<User> filterUserBasedOnId(List<User> users, String id){
    return users.stream()
                .filter( user -> !user.getUniqid().equals(id))
                .collect(Collectors.toList());
}

Edit: You can have User class like this

public class User {
  private String uniqid;
  private String name;
  private String address;
  private String email;
  private String mobile;
  private String city;
  // getter, setter
}

To get the List<User>, you need to parse it from json. You can refer to this

Tuan Hoang
  • 586
  • 1
  • 7
  • 14
  • Hi How To Get **List** Because I Have Response in `SharedPreferences sharedPreferences = getSharedPreferences("Users", MODE_PRIVATE); String response = sharedPreferences.getString("response", null);` How To Do With This Response Please Update Answer With This Thanks – new Devloper Aug 12 '20 at 04:09