如何从JSON文件中删除一个项目,使用列表视图中单击项目的UniqueId

本文关键字:项目 UniqueId 列表 单击 视图 一个 JSON 文件 删除 | 更新日期: 2023-09-27 18:12:56

我有一个JSON文件,像这样:

{
"Groups": [
    {
        "UniqueId": "1",
        "Name": "England",
        "Members": [
            {
                "UniqueId": "Rooney",
                "Name": "Rooney",
                "JerseyNumber": "10",
                "Position": "Forward"
            },
            {
                "UniqueId": "Aquero",
                "Name": "Aguero",
                "JerseyNumber": "16",
                "Position": "Forward"
            },
            {
                "UniqueId": "Nani",
                "Name": "Nani",
                "JerseyNumber": "7",
                "Position": "Midfielder"
            }
            ]
        }
    ]
}

我已经能够通过以下代码到达JSON的Members数组:

StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("Data2.json");
string jsonText = await FileIO.ReadTextAsync(file);    
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonArray jsonArray = jsonObject["Groups"].GetArray()[0].GetObject()["Members"].GetArray();

我有一个成员的UniqueId,我想用UniqueId搜索该成员在"Members"数组中的位置,然后我想删除该成员。假设我有UniqueId ="Nani",我想搜索具有唯一Id Nani的成员并删除它。

我希望JSON文件中的最终结果为:

{
"Groups": [
    {
        "UniqueId": "1",
        "Name": "England",
        "Members": [
            {
                "UniqueId": "Rooney",
                "Name": "Rooney",
                "JerseyNumber": "10",
                "Position": "Forward"
            },
            {
                "UniqueId": "Aquero",
                "Name": "Aguero",
                "JerseyNumber": "16",
                "Position": "Forward"
            }
            ]
        }
    ]
}

如何从JSON文件中删除一个项目,使用列表视图中单击项目的UniqueId

我认为创建一个与json树匹配的类会更好。我正在使用。net 3.5,所以我想我错过了你正在使用的json类的dll。但是你会得到下面代码中的逻辑,注释也会帮助你

    public class Groups // "Groups": [
    {
        public int UniqueId { get; set; } // "UniqueId": "1",
        public String Name { get; set; } //  "Name": "England",
        public List<Member> ListMembers { get; set; } // "Members": [

        public Groups(string json)
        {
            /* use your json object to get different data */
            UniqueId = "Group UniqueId";
            Name = "Group Name";
            // get all member
            foreach (jsonMember in jsonObject.Members)
            {
                Member member = new Member
                {
                    UniqueId = "jsonMember UniqueId",
                    Name = "jsonMember Name",
                    JerseyNumber = "jsonMember JerseyNumber",
                    Position = "jsonMember Position",
                };
                ListMembers.Add(member);
            }
        }
        public class Member // {"UniqueId":"Nani", "Name":"Nani", ... }
        {
            public string UniqueId { get; set; } // "UniqueId": "Aquero",
            public String Name { get; set; } // "Name": "Aguero",
            public int JerseyNumber { get; set; } // "JerseyNumber": "16",
            public string Position { get; set; } // "Position": "Forward"
        }

        public void Delete(string UniqueId) // by example Delete("Nani")
        {
            ListMembers.RemoveAll(m => m.UniqueId == UniqueId);
        }
    }