C# 更新json对象w/json.net

C# 更新json对象w/json.net,c#,json.net,C#,Json.net,我的Json文件有以下类: using System.Collections.Generic; namespace MITSonWald { public class ExtLine { public List<Dictionary<string, List<LineList>>> LineList { get; set; } } public class LineList { pub

我的Json文件有以下类:

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; }
    }
}
我想向设备列表添加一个对象,但无法完成

我尝试的

/* 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);
/*反序列化Json文件*/
动态json=
JsonConvert.DeserializeObject(
File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory+“cfg\\lines.json”);
List myDevice=新列表
{
新词典
{
{
"192.168.10.205",
新名单
{
新装置
{
Name=“Zimmer2”,
Volume=“5”
}
}
}
}
};
json.LineList[0][“TapiLine22”][0].DeviceList.Add(myDevice);
抛出异常(Google从德语翻译)

其他信息:重载System.Collections.Generic.List的最佳匹配。Add(System.Collections.Generic.Dictionary)方法包含一些无效参数。

从异常情况看,您的Add需要:

Add (Dictionary<string, List<MITSonWald.Device>>)
因为您的myDevice与DeviceList的类型相同

您还可以创建字典并将其添加到DeviceList(只需抛出不需要的列表):

var myDevice=新字典
{
{
"192.168.10.205",
新名单
{
新装置
{
Name=“Zimmer2”,
Volume=“5”
}
}
}
}

是的,发布异常是值得的——至少异常类型会很有用,而且消息总是有谷歌翻译。理想情况下,提供一个测试,以便我们可以自己尝试。。。(你现在真的在解析任何JSON吗?听起来你还没有达到这一部分的要求……)这段代码编译得好吗?请发布例外情况。您不应该先将您的
myDevice
添加到集合中,然后将其转换为json吗?什么类型的
json
?稍微更新了问题并添加了例外
json
是动态的。代码编译得很好,调用函数时会引发异常。我可以在哪里在线发布工作示例?有什么东西像.net的JSFIDLE吗?我认为
JsonConvert.DeserializeObject
应该是
JsonConvert.DeserializeObject
对吗?因为您正在访问属性
DeviceList
,该属性未在
ExtLine
json.LineList[0][“TapiLine22”][0]中声明。DeviceList=myDevice确实可以工作,但会替换现有设备。我不明白第二部分,那正是我在做的?(这导致了异常)。
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.
Add (Dictionary<string, List<MITSonWald.Device>>)
List<Dictionary<string, List<Device>>>
json.LineList[0]["TapiLine22"][0].DeviceList = myDevice;
var myDevice = new Dictionary<string, List<Device>>
{
    {
        "192.168.10.205",
        new List<Device>
        {
            new Device
            {
                Name = "Zimmer2",
                Volume = "5"
            }
        }
    }
}