我正在尝试使用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的产品创建的。
问候
史蒂夫
发布于 2014-06-26 14:06:46
你的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; }
}https://stackoverflow.com/questions/24432567
复制相似问题