1

I'm trying to read the following json file using json.net, but I cant seem to make it work

{
    "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"
            }
        }
    }
}

I have tried to use http://json2csharp.com/ to create the c# code but this fails to work. I get the feeling the json is invalid on the lines that start "DVB-C #2", "DVB-T #1" and "DVB-S #0".

I'm using this command to attempt Deserialize the string "JsonConvert.DeserializeObject(json)"

Can anyone verify if it can be done?

p.s. the json is created by a product called tvheadend.

Regards

Steve

Steve Parry
  • 366
  • 3
  • 12
  • 4
    If you are ever doubting your JSON returns, you could always use something like http://www.jsonlint.com to validate it... – Dylan Corriveau Jun 26 '14 at 14:00
  • Check it [here](http://json.parser.online.fr/). – EKrueger Jun 26 '14 at 14:01
  • 1
    According to the link Dylan provided, your JSON is valid. Json2CSharp was able to generate C# classes, but C# classes/properties can't contain a `#` sign in the name (which is ironic!). That's why it says invalid name. So you just have to decorate it with an attribute `[[JsonProperty(PropertyName = "DVB-S #0")]`. See [this question](http://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net). – mason Jun 26 '14 at 14:04
  • @mason yup, you are right. I was going to update my comment, but you beat me to it. Yea, the # character is not valid in Json.NET I believe as well... – Dylan Corriveau Jun 26 '14 at 14:10

1 Answers1

2

Your json is valid. But seems like you have problem with property names like DVB-T #1 (not a valid c# identifier). if you know the property names beforehand, you can use JsonProperty attribute. But in your case they seem to be dynamic. So you can use a Dictionary in that case

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

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

your Item class can be something like this:

(I used json2charp and some part of your 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; }
}
L.B
  • 114,136
  • 19
  • 178
  • 224