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 .
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) {
}
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
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.