從組合框中選擇并單擊按鈕后,我想從 json 中洗掉物件。例如,我從組合框中選擇了 Harry Thomas,然后單擊按鈕 Person Harry Thomas 應該被洗掉。我只從 json 中洗掉物件有問題。我試過這樣的事情:
for (int i = 0; i < result.Person.Count; i)
{
if (combobox1.Text == result.Person[i].Name " " result.Person[i].Surname)
{
result.Person[i].Remove();
}
}
Json 和類:
{
"Person": [
{
"Speciality": "Archer",
"Id": 432742,
"Name": "Charlie",
"Surname": "Evans",
"Items": [
"Bow",
"Arrow",
]
},
{
"Speciality": "Soldier",
"Id": 432534,
"Name": "Harry",
"Surname": "Thomas",
"Items": [
"Gun",
"Knife",
]
}
],
"Monster": [
{
"Name": "Papua",
"Skills": [
"Jump",
"SlowWalk",
]
},
{
"Name": "Geot",
"Skills": [
"Jump",
"SlowWalk",
]
}
]
}
public class Person
{
public string Speciality { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public List<string> Items { get; set; }
}
public class Monster
{
public string Name { get; set; }
public List<string> Skills { get; set; }
}
public class Root
{
public List<Person> Person { get; set; }
public List<Monster> Monster { get; set; }
}
如何從json檔案中洗掉物件?提前致謝。
uj5u.com熱心網友回復:
嘗試這個
var result =JsonConvert.DeserializeObject<Root>(json);
var name="Harry"; // replace with combobox
var surname="Thomas"; // replace with combobox
result.Person.RemoveAll(i=> i.Name==name && i.Surname==surname);
uj5u.com熱心網友回復:
可以嘗試類似的東西
result.Person = result.Person.Where(item=> combobox1.Text != $"{item.Name} {item.Sorname}").ToList()
然后將結果串列保存在 json 檔案中
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429789.html
下一篇:決議JSON字串時出現問題
