我有一个像这样的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"
}
]
}
]
}
我已经能够通过以下代码深入到Members
JSON数组:
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"
}
]
}
]
}
答案 0 :(得分:0)
我认为创建一个与json树匹配的类会更好。我正在使用.net 3.5所以我想我想念你正在使用的json课程。但是你将在下面的代码中得到逻辑,评论也会帮助你
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);
}
}