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

huangapple go评论128阅读模式
英文:

How do I Remove a User from a Volley JSON Response?

问题

Sure, here's the translated code part:

  1. 我想要通过其唯一标识uniqid删除特定用户我已经从Volley响应中解析了JSON数据以下是我的响应
  2. 这是我的响应数据
  3. [
  4. {
  5. "uniqid":"h5faweWtd",
  6. "name":"Test_1",
  7. "address":"tst",
  8. "email":"ru_tst@tst.cc",
  9. "mobile":"12345",
  10. "city":"indd"
  11. },{
  12. "uniqid":"h5Wtd",
  13. "name":"Test_2",
  14. "address":"tst",
  15. "email":"ru_tst@tst.cc",
  16. "mobile":"1235945",
  17. "city":"indd4"
  18. },{
  19. "uniqid":"h5Wtd25",
  20. "name":"Test_3",
  21. "address":"tdsdsst",
  22. "email":"rsfu_tst@tsfst.cc",
  23. "mobile":"1263345",
  24. "city":"indd9"
  25. }
  26. ]
  27. 我想要通过其唯一标识uniqid删除特定用户
  28. PHP我可以轻松地使用以下代码来实现
  29. foreach ($UserList as $key => $value) {
  30. if (in_array('uniqid', $uniqid_To_Remove)) {
  31. unset($UserList[$key]);
  32. }
  33. }
  34. $UserList = json_encode($UserList);
  35. echo $UserList;
  36. 但我想要在Android Java中获取响应并将其保存到共享首选项SharedPreferences当有互联网连接时以及当没有互联网连接时我想要从共享首选项中获取响应数据并根据其唯一标识uniqid删除特定用户如何在Android Java中实现这个功能
  37. 我正在尝试删除整个用户问题是对象的位置不是固定的它可以在数组中的任何位置唯一固定的是我有的是uniqid
英文:

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

this is my response:

  1. [
  2. {
  3. "uniqid":"h5faweWtd",
  4. "name":"Test_1",
  5. "address":"tst",
  6. "email":"ru_tst@tst.cc",
  7. "mobile":"12345",
  8. "city":"indd"
  9. },{
  10. "uniqid":"h5Wtd",
  11. "name":"Test_2",
  12. "address":"tst",
  13. "email":"ru_tst@tst.cc",
  14. "mobile":"1235945",
  15. "city":"indd4"
  16. },{
  17. "uniqid":"h5Wtd25",
  18. "name":"Test_3",
  19. "address":"tdsdsst",
  20. "email":"rsfu_tst@tsfst.cc",
  21. "mobile":"1263345",
  22. "city":"indd9"
  23. }
  24. ]

I want to remove specific user with its uniquid

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

  1. foreach ($UserList as $key => $value) {
  2. if (in_array('uniqid', $uniqid_To_Remove)) {
  3. unset($UserList[$key]);
  4. }
  5. }
  6. $UserList = json_encode($UserList);
  7. 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.

答案1

得分: 0

以下是翻译好的内容:

要对一个 JsonArray 进行过滤,你可以尝试这样做:

  1. try {
  2. List<JSONObject> filtered = new ArrayList<>();
  3. JSONArray array = new JSONArray(); // 用实际的响应数组替换这里的 array
  4. for (int i = 0; i < array.length(); i++) {
  5. JSONObject obj = array.getJSONObject(i);
  6. String id = obj.getString("uniqid");
  7. if (!id.equals("h5Wtd25")) { // 根据你的过滤规则进行替换
  8. filtered.add(obj);
  9. }
  10. }
  11. } catch (JSONException e) {
  12. }
英文:

To filter a JsonArray, you could try this:

  1. try {
  2. List&lt;JSONObject&gt; filtered = new ArrayList&lt;&gt;();
  3. JSONArray array = new JSONArray(); //Replace array with Response array
  4. for (int i = 0; i &lt; array.length(); i++) {
  5. JSONObject obj = array.getJSONObject(i);
  6. String id = obj.getString(&quot;uniqid&quot;);
  7. if (!id.equals(&quot;h5Wtd25&quot;)) { //Replace with your own filtered rule
  8. filtered.add(obj);
  9. }
  10. }
  11. } catch (JSONException e) {
  12. }

答案2

得分: 0

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&lt;User&gt;, you need to parse it from json. You can refer to this

英文:

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

  1. private List&lt;User&gt; filterUserBasedOnId(List&lt;User&gt; users, String id){
  2. return users.stream()
  3. .filter( user -&gt; !user.getUniqid().equals(id))
  4. .collect(Collectors.toList());
  5. }

Edit: You can have User class like this

  1. public class User {
  2. private String uniqid;
  3. private String name;
  4. private String address;
  5. private String email;
  6. private String mobile;
  7. private String city;
  8. // getter, setter
  9. }

To get the List&lt;User&gt;, you need to parse it from json. You can refer to this

huangapple
  • 本文由 发表于 2020年8月12日 11:14:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63369222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定