1

I have the following class for my Json file:

using System.Collections.Generic;

namespace MITSonWald
{
    public class ExtLine
    {
        public List<Dictionary<string, List<LineList>>> LineList { get; set; }
    }

    public class LineList
    {
        public List<Dictionary<string, List<Device>>> DeviceList { get; set; }
    }

    public class Device
    {
        public string Volume { get; set; }
        public string Name { get; set; }
    }
}

Resulting Json File

{
  "LineList":[
    {
      "TapiLine22":[
        {
          "DeviceList":[
            {
              "192.168.10.204":[
                {
                  "Volume":"5",
                  "Name":"Büro"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

I would like to add an object to the DeviceList but I can't get it done.

What I tried

/* Deserialize Json File */
dynamic json =
    JsonConvert.DeserializeObject<ExtLine>(
           File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "cfg\\lines.json"));

List<Dictionary<string, List<Device>>> myDevice = new List<Dictionary<string, List<Device>>>
{
    new Dictionary<string, List<Device>>
    {
        {
            "192.168.10.205",
            new List<Device>
            {
                new Device
                {
                    Name = "Zimmer2",
                    Volume = "5"
                }
            }
        }
    }
};

json.LineList[0]["TapiLine22"][0].DeviceList.Add(myDevice);

Thrown Exception (Google Translate from German)

Additional Information: The best match for the overloaded System.Collections.Generic.List <System.Collections.Generic.Dictionary <string, System.Collections.Generic.List <MITSonWald.Device >>>. Add (System.Collections.Generic. Dictionary <string, System.Collections.Generic.List <MITSonWald.Device >>) method contains some invalid arguments.
5
  • Yes, it's worth posting the exception - at least the exception type will be useful, and there's always Google Translate for the message. Ideally, provide a minimal reproducible example so we can try this for ourselves... (Are you actually parsing any JSON at the moment? It sounds like you're not up to that part yet...) Commented Nov 16, 2016 at 9:07
  • Did this code compiled fine? Please post the exception as well. Shouldn't you be adding your myDevice to the collection first and then convert it to json instead ? Commented Nov 16, 2016 at 9:09
  • what type is json of? Commented Nov 16, 2016 at 9:10
  • Updated question a little and added exception. json is dynamic. The code compiles fine, exception is thrown when i call the function. Where can I post a working example online? Is there sth. like jsfiddle for .net? Commented Nov 16, 2016 at 9:20
  • I think JsonConvert.DeserializeObject<ExtLine> should be JsonConvert.DeserializeObject<LineList> right? because you are accessing the property DeviceList which is not declared in ExtLine Commented Nov 16, 2016 at 9:35

1 Answer 1

1

From the exception it looks like your Add is expecting:

Add (Dictionary<string, List<MITSonWald.Device>>)

but you are adding object of type

List<Dictionary<string, List<Device>>>

This should work(but it will replace your list):

json.LineList[0]["TapiLine22"][0].DeviceList = myDevice;

because your myDevice is the same type as DeviceList.

You could also just create dictionary and add it to DeviceList(just throw unneeded list):

var myDevice = new Dictionary<string, List<Device>>
{
    {
        "192.168.10.205",
        new List<Device>
        {
            new Device
            {
                Name = "Zimmer2",
                Volume = "5"
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

json.LineList[0]["TapiLine22"][0].DeviceList = myDevice; works indeed but replaces the existing device. I don't understand the second part, that's exactly what I'm doing? (Which causes the exception).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.