0

I have a json file that looks like this:

Json

{
"Telephones": [
{
  "TapiLine": "XX Line",
  "SpeakerList": [
    {
      "Name": "Office",
      "Ip": "192.168.10.204",
      "Volume": "5"
    },
    {
      "Name": "Living",
      "Ip": "192.168.10.214",
      "Volume": "5"
    }
  ]
}
]
}

Class

class Result
{
    public List<Telephone> Telephones { get; set; }

    public Result()
    {
        Telephones = new List<Telephone>();
    }
}


class Telephone
{
    public string TapiLine { get; set; }
    public List<Speakers> SpeakerList { get; set; }

    public Telephone()
    {
        SpeakerList = new List<Speakers>();
    }
}

class Speakers
{

    public string Name { get; set; }
    public string Ip { get; set; }
    public string Volume { get; set; }

}

What I'd like to do

I have a combination of TapiLine and Ip and I would like to remove that Object with the according Ip from the SpeakerList.

What I have already

foreach (var telephones in json.Telephones.ToArray())
{
if (telephones.TapiLine == tLine.Name)
{
    foreach (var speakers in telephones.SpeakerList)
    {

        if (speakers.Ip == CurrentEditIp)
        {
            Console.WriteLine("REMOVE ME: " + speakers.Ip + " FROM: " + tLine.Name);

            //UNTIL HERE IT'S FINE; THE REST IS JUST GUESSING...
            var docsToRemove = new Result
            {
                Telephones = new List<Telephone>
                {
                    new Telephone
                    {
                        TapiLine = tLine.Name,
                        SpeakerList = new List<Speakers>
                        {
                            new Speakers
                            {
                                Name = CurrentEditName,
                                Ip = CurrentEditIp,
                                Volume = "5"
                            }
                        }
                    }
                }
            };

            json.Remove(docsToRemove); //THIS DOES NOTHING
        }

    }

}
}

How could I remove the according object from SpeakerList? Any hint appreciated!

1 Answer 1

1

You can remove an object from a collection by using methods such as Remove, RemoveAt, RemoveAll or RemoveRange.

Check the below code snippets demonstrating it using a for loop iterating backward or by using LINQ.

  1. Remove Object From JSON using for loop (compile C# online link)

    namespace JSON
    {
        using Newtonsoft.Json;
        using System.Collections.Generic;
        using System.Linq;
    
        class Program
        {
            static void Main(string[] args)
            {
                var json = "{\"Telephones\": [{ \"TapiLine\": \"XX Line\", \"SpeakerList\": [{ \"Name\": \"Office\", \"Ip\": \"192.168.10.204\", \"Volume\": \"5\" }, { \"Name\": \"Living\", \"Ip\": \"192.168.10.214\", \"Volume\": \"5\" }] }] }";
                var result = JsonConvert.DeserializeObject(json, typeof(Result)) as Result;
    
                for (int i = result.Telephones.Count - 1; i >= 0; i--)
                {
                    if (result.Telephones[i].TapiLine == "XX Line")
                    {
                        for (int j = result.Telephones[i].SpeakerList.Count - 1; j >= 0; j--)
                        {
                            if (result.Telephones[i].SpeakerList[j].Ip == "192.168.10.204")
                            {
                                result.Telephones[i].SpeakerList.RemoveAt(j);
                            }
                        }
                    }
                }
    
                json = JsonConvert.SerializeObject(result);
                // write updated JSON back to the file
            }
        }
    
        class Result
        {
            public List<Telephone> Telephones { get; set; }
    
            public Result()
            {
                Telephones = new List<Telephone>();
            }
        }    
    
        class Telephone
        {
            public string TapiLine { get; set; }   
            public List<Speakers> SpeakerList { get; set; }
    
            public Telephone()
            {
                SpeakerList = new List<Speakers>();
            }
        }
    
        class Speakers
        {
            public string Name { get; set; }    
            public string Ip { get; set; }
            public string Volume { get; set; }    
        }
    }
    
  2. Remove Object From JSON using LINQ (compile C# online link)

    namespace JSON
    {
        using Newtonsoft.Json;
        using System.Collections.Generic;
        using System.Linq;
    
        class Program
        {
            static void Main(string[] args)
            {
                var json = "{\"Telephones\": [{ \"TapiLine\": \"XX Line\", \"SpeakerList\": [{ \"Name\": \"Office\", \"Ip\": \"192.168.10.204\", \"Volume\": \"5\" }, { \"Name\": \"Living\", \"Ip\": \"192.168.10.214\", \"Volume\": \"5\" }] }] }";
                var result = JsonConvert.DeserializeObject(json, typeof(Result)) as Result;
                result
                    .Telephones
                    .Where(x => x.TapiLine == "XX Line")
                    .ToList()
                    .ForEach(x => x.SpeakerList.RemoveAll(y => y.Ip == "192.168.10.204"));
    
                json = JsonConvert.SerializeObject(result);
                // write updated JSON back to the file
            }
        }
    
        class Result
        {
            public List<Telephone> Telephones { get; set; }
    
            public Result()
            {
                Telephones = new List<Telephone>();
            }
        }
    
        class Telephone
        {
            public string TapiLine { get; set; }    
            public List<Speakers> SpeakerList { get; set; }
    
            public Telephone()
            {
                SpeakerList = new List<Speakers>();
            }
        }
    
        class Speakers
        {
            public string Name { get; set; }   
            public string Ip { get; set; }
            public string Volume { get; set; }    
        }
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Took the first one, but I'm sure 2. option was also working!

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.