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

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

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

问题

Sure, here's the translated code part:

我想要通过其唯一标识uniqid删除特定用户我已经从Volley响应中解析了JSON数据以下是我的响应

这是我的响应数据

[
     {
        "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"
      }
    ]

我想要通过其唯一标识uniqid删除特定用户

在PHP中我可以轻松地使用以下代码来实现

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

echo $UserList;

但我想要在Android Java中获取响应并将其保存到共享首选项SharedPreferences当有互联网连接时以及当没有互联网连接时我想要从共享首选项中获取响应数据并根据其唯一标识uniqid删除特定用户如何在Android Java中实现这个功能

我正在尝试删除整个用户问题是对象的位置不是固定的它可以在数组中的任何位置唯一固定的是我有的是uniqid
英文:

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.

答案1

得分: 0

以下是翻译好的内容:

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

try {
    List<JSONObject> filtered = new ArrayList<>();
    JSONArray array = new JSONArray(); // 用实际的响应数组替换这里的 array

    for (int i = 0; i < array.length(); i++) {
        JSONObject obj = array.getJSONObject(i);
        String id = obj.getString("uniqid");
        if (!id.equals("h5Wtd25")) { // 根据你的过滤规则进行替换
            filtered.add(obj); 
        }
    }
} catch (JSONException e) {

}
英文:

To filter a JsonArray, you could try this:

       try {
List&lt;JSONObject&gt; filtered = new ArrayList&lt;&gt;();
JSONArray array = new JSONArray(); //Replace array with Response array
for (int i = 0; i &lt; array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
String id = obj.getString(&quot;uniqid&quot;);
if (!id.equals(&quot;h5Wtd25&quot;)) { //Replace with your own filtered rule
filtered.add(obj); 
}
}
} catch (JSONException e) {
}

答案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

private List&lt;User&gt; filterUserBasedOnId(List&lt;User&gt; users, String id){
return users.stream()
.filter( user -&gt; !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

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:

确定