簡體   English   中英

如何從 Volley JSON 響應中刪除用戶?

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

我想刪除具有唯一性的特定用戶,我已經解析了 json_encode($data); 來自凌空回應:

這是我的回應:

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

我想用它的 uniquid 刪除特定用戶

借助以下代碼,我將在 php 中輕松完成:

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

echo $UserList;

但是我想在互聯網可用和互聯網不可用時獲得響應並將其保存到共享首選項:然后我想從共享首選項中獲取響應數據並從其 uniqid 中刪除特定用戶。 如何在 android java 中做到這一點?

我正在嘗試刪除整個用戶。 問題是 object position 不是 static,它可以在數組中的任何位置。 我唯一擁有的是 static 是uniqid

要過濾 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) {

        }

假設您有User列表,您可以使用filter獲取新列表,該列表刪除具有特定 id 的用戶

private List<User> filterUserBasedOnId(List<User> users, String id){
    return users.stream()
                .filter( user -> !user.getUniqid().equals(id))
                .collect(Collectors.toList());
}

編輯:您可以像這樣擁有用戶 class

public class User {
  private String uniqid;
  private String name;
  private String address;
  private String email;
  private String mobile;
  private String city;
  // getter, setter
}

要獲取List<User> ,您需要從 json 解析它。 你可以參考這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM