【发布时间】: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 中也无效......