繁体   English   中英

我可以使用json.net导入此JSON字符串吗

[英]Can I import this JSON string using json.net

我正在尝试使用json.net读取以下json文件,但是我似乎无法使其工作

{
    "rootpath": "/dev/dvb/adapter1",
    "frontends": {
        "DVB-C #2": {
            "powersave": false,
            "enabled": false,
            "priority": 0,
            "displayname": "Sony CXD2820R (DVB-C) : DVB-C #1",
            "networks": [
            ],
            "type": "DVB-C"
        },
        "DVB-T #1": {
            "powersave": false,
            "enabled": true,
            "priority": 0,
            "displayname": "Sony CXD2820R (DVB-T/T2) : DVB-T #0",
            "networks": [
                "5225c9f02c93f1cbe9ae35e5bbe6007f"
            ],
            "type": "DVB-T"
        },
        "DVB-S #0": {
            "powersave": false,
            "enabled": false,
            "priority": 0,
            "displayname": "Conexant CX24116/CX24118 : DVB-S #0",
            "networks": [
            ],
            "type": "DVB-S",
            "satconf": {
                "type": "simple",
                "diseqc_repeats": 0,
                "lnb_poweroff": false,
                "elements": [
                    {
                        "enabled": false,
                        "priority": 0,
                        "networks": [
                        ],
                        "lnb_type": "Universal",
                        "uuid": "2db1bb45f2ac9ae5caa63367674caafb",
                        "lnb_conf": {
                        }
                    }
                ],
                "uuid": "94833aabc581ce96d75bb6884a05f20a"
            }
        }
    }
}

我尝试使用http://json2csharp.com/创建c#代码,但这无法正常工作。 我感到在以“ DVB-C#2”,“ DVB-T#1”和“ DVB-S#0”开头的行上json无效。

我正在使用此命令尝试反序列化字符串“ JsonConvert.DeserializeObject(json)”

任何人都可以验证是否可以完成吗?

ps json是由称为tvheadend的产品创建的。

问候

史蒂夫

您的json有效。 但是似乎您对DVB-T #1这样的属性名称有DVB-T #1 (不是有效的c#标识符)。 如果事先知道属性名称,则可以使用JsonProperty属性。 但是在您的情况下,它们似乎是动态的。 因此,在这种情况下,您可以使用字典

var obj = JsonConvert.DeserializeObject<Root>(json);

public class Root
{
    public string rootpath { set; get; }
    public Dictionary<string, Item> frontends { set; get; }
}

您的Item类可以是这样的:

(我使用了json2charp和json的某些部分( DVB-S #0:{this part} ))

public class LnbConf
{
}

public class Element
{
    public bool enabled { get; set; }
    public int priority { get; set; }
    public List<object> networks { get; set; }
    public string lnb_type { get; set; }
    public string uuid { get; set; }
    public LnbConf lnb_conf { get; set; }
}

public class Satconf
{
    public string type { get; set; }
    public int diseqc_repeats { get; set; }
    public bool lnb_poweroff { get; set; }
    public List<Element> elements { get; set; }
    public string uuid { get; set; }
}

public class Item
{
    public bool powersave { get; set; }
    public bool enabled { get; set; }
    public int priority { get; set; }
    public string displayname { get; set; }
    public List<object> networks { get; set; }
    public string type { get; set; }
    public Satconf satconf { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM