【问题标题】:Can I import this JSON string using json.net我可以使用 json.net 导入这个 JSON 字符串吗
【发布时间】:2014-06-26 13:59:26
【问题描述】:

我正在尝试使用 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# 代码,但这无法正常工作。我感觉 json 在以“DVB-C #2”、“DVB-T #1”和“DVB-S #0”开头的行上是无效的。

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

谁能验证它是否可以完成?

附言json 是由名为 tvheadend 的产品创建的。

问候

史蒂夫

【问题讨论】:

  • 如果你怀疑你的 JSON 返回,你总是可以使用类似 jsonlint.com 的东西来验证它......
  • 查看here
  • 根据 Dylan 提供的链接,您的 JSON 是有效的。 Json2CSharp 能够生成 C# 类,但 C# 类/属性不能在名称中包含 # 符号(具有讽刺意味!)。这就是为什么它说无效名称。所以你只需要用属性[[JsonProperty(PropertyName = "DVB-S #0")] 来装饰它。见this question
  • @mason 是的,你是对的。我打算更新我的评论,但你打败了我。是的,我相信 # 字符在 Json.NET 中也无效......

标签: c# json json.net


【解决方案1】:

你的 json 是有效的。但似乎您对 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; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多