0

I want to remove object from json after select from combobox and click button. For example i selected Harry Thomas from combobox and after click on the button Person Harry Thomas should be removed. I have problem with remove object from json only. I tried something like this:

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 and classes:

{
  "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; }
}

How to remove object from json file? Thanks in advance.

5
  • "i got error" - it would be great to know what kind of error. Please always include that information Commented Feb 20, 2022 at 20:30
  • 3
    Likely it's because you're changing the collection while iterating over it. While looping over the collection, create a new collection that contains all items that shall be removed. Then loop over that collection and remove each item from the original list. Commented Feb 20, 2022 at 20:32
  • ErrorCode is CS1061 learn.microsoft.com/en-us/dotnet/csharp/language-reference/… Commented Feb 20, 2022 at 20:33
  • 2
    The error is because Person does not have a .Remove() method. You need to remove the person from a list, so call the remove method at the list, not at the person (result.Person.Remove(Person[i]);). But next you'll run into the problem I mentioned before. Commented Feb 20, 2022 at 20:39
  • 1
    Try result.Person.RemoveAt(i); Commented Feb 20, 2022 at 21:32

2 Answers 2

1

try this

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);
Sign up to request clarification or add additional context in comments.

Comments

1

May try something like

result.Person = result.Person.Where(item=> combobox1.Text != $"{item.Name} {item.Sorname}").ToList()

and save the result list in the json file after that

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.