英文:
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<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) {
}
答案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<User>
, 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<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论