简体   繁体   English

如何从 Volley JSON 响应中删除用户?

[英]How do I Remove a User from a Volley JSON Response?

i want to remove specific user with its uniquid i have parse json_encode($data);我想删除具有唯一性的特定用户,我已经解析了 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我想用它的 uniquid 删除特定用户

I'll easily do it in php with the help of this code:借助以下代码,我将在 php 中轻松完成:

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.但是我想在互联网可用和互联网不可用时获得响应并将其保存到共享首选项:然后我想从共享首选项中获取响应数据并从其 uniqid 中删除特定用户。 how to do it in android java?如何在 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.问题是 object position 不是 static,它可以在数组中的任何位置。 the only thing that is static that i have is uniqid .我唯一拥有的是 static 是uniqid

To filter a JsonArray, you could try this:要过滤 JsonArray,你可以试试这个:

       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假设您有User列表,您可以使用filter获取新列表,该列表删除具有特定 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编辑:您可以像这样拥有用户 class

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.要获取List<User> ,您需要从 json 解析它。 You can refer to this你可以参考这个

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

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