【发布时间】:2014-04-08 22:56:59
【问题描述】:
首先,我对 Json.Net 没有太多经验,所以我提前道歉。我可以很好地反序列化基本对象,但这是我遇到麻烦的地方......
我有这个疯狂的返回 JSON,我不知道如何反序列化。我已经把它贴在下面了。我正在使用 C# 和 Newtonsoft.Json (Json.Net) NuGet 包来编写一个应用程序来使用这个 JSON。
基本上,JSON 由“类型”(“D”、“M”、“O”、“R”、“G”或“A”)分割,并且该类型具有嵌套的 JSON 对象( "IO") 带有一堆关于类型状态的相关信息。 IO 数据是一个 List。
[
{
"Type":"D",
"IO":[
{
"#":1,
"Desc":"Level",
"SC":583,
"DT":"Tuesday - 4/8/14 at 5:03 AM",
"InAlarm":false
},
{
"#":2,
"Desc":"Input 2 ",
"SC":348,
"DT":"Tuesday - 4/8/14 at 8:05 AM",
"InAlarm":false
},
{
"#":3,
"Desc":"Input 3 ",
"SC":214,
"DT":"Tuesday - 4/8/14 at 9:05 AM",
"InAlarm":false
},
{
"#":4,
"Desc":"Input 4 ",
"SC":180,
"DT":"Tuesday - 4/8/14 at 6:24 AM",
"InAlarm":false
},
{
"#":8,
"Desc":"Input 8 ",
"SC":296,
"DT":"Tuesday - 9/18/12 at 3:20 PM",
"InAlarm":false
},
{
"#":9,
"Desc":"Input 9 ",
"SC":282,
"DT":"Tuesday - 9/18/12 at 3:20 PM",
"InAlarm":false
},
{
"#":10,
"Desc":"Input 10",
"SC":406,
"DT":"Tuesday - 9/18/12 at 3:20 PM",
"InAlarm":false
},
{
"#":11,
"Desc":"Input 11",
"SC":246,
"DT":"Wednesday - 1/15/14 at 2:36 PM",
"InAlarm":false
}
]
},
{
"Type":"M",
"IO":[
{
"#":5,
"Desc":"Input 5 ",
"VS":"Style: Rain"
},
{
"#":6,
"Desc":"Input 6 ",
"VS":"Style: Counter"
},
{
"#":7,
"Desc":"Input 7 ",
"VS":"Style: Counter"
}
]
},
{
"Type":"R",
"IO":[
{
"#":12,
"Desc":"Pump 1 Start Failure",
"SC":952,
"DT":"Thursday - 2/6/14 at 11:23 AM",
"InAlarm":false
},
{
"#":13,
"Desc":"Pump 2 Start Failure",
"SC":960,
"DT":"Thursday - 2/6/14 at 12:06 PM",
"InAlarm":false
},
{
"#":14,
"Desc":"high level Start Failure",
"SC":936,
"DT":"Thursday - 2/6/14 at 12:49 PM",
"InAlarm":false
}
]
},
{
"Type":"G",
"IO":[
{
"Desc":"Primary Power",
"SC":28,
"DT":"Tuesday - 4/8/14 at 7:04 AM",
"InAlarm":false,
"VS":"Present"
},
{
"Desc":"Battery Status",
"SC":26,
"DT":"Monday - 4/7/14 at 12:05 PM",
"InAlarm":false,
"VS":"13.25 Volts"
},
{
"Desc":"Signal Strength",
"SC":2,
"DT":"Wednesday - 4/2/14 at 6:21 AM",
"InAlarm":false,
"VS":"-70 db"
},
{
"Desc":"Maintenance Key",
"SC":12,
"DT":"Thursday - 4/3/14 at 2:00 PM",
"InAlarm":false,
"VS":"Enabled"
},
{
"Desc":"Communication Check",
"SC":49,
"DT":"Thursday - 4/3/14 at 5:02 AM",
"InAlarm":false,
"VS":"State Change"
}
]
},
{
"Type":"A",
"IO":[
{
"#":1,
"Desc":"Analog 1",
"SC":568,
"DT":"Tuesday - 4/8/14 at 9:24 AM",
"InAlarm":0,
"VS":"6.60 ft"
},
{
"#":2,
"Desc":"Analog 2 ",
"SC":8,
"DT":"Thursday - 6/10/10 at 2:15 PM",
"InAlarm":0,
"VS":"13.00 ft"
},
{
"#":3,
"Desc":"Analog 3 ",
"SC":12,
"DT":"Monday - 6/21/10 at 11:20 AM",
"InAlarm":0,
"VS":"13.00 ft"
},
{
"#":4,
"Desc":"Analog 4 ",
"SC":4,
"DT":"Monday - 6/14/10 at 11:11 AM",
"InAlarm":0,
"VS":"12.90 ft"
}
]
},
{
"Type":"O",
"IO":[
{
"#":1,
"Desc":"Output 1",
"SC":0,
"DT":"NA",
"VS":"Automatic"
},
{
"#":3,
"Desc":"Output 3",
"SC":0,
"DT":"NA",
"VS":"Automatic"
},
{
"#":2,
"Desc":"Output 2",
"SC":0,
"DT":"NA",
"VS":"Automatic"
},
{
"#":4,
"Desc":"Output 4",
"SC":0,
"DT":"NA",
"VS":"In Hand - Off"
}
]
}
]
我想要一个名为 Status 的对象,它将包含每个“类型”(D、M、O、R、G 或 A)的字段。下面是我的示例类,其中“D”类型是数字类,“M”类型是监控类。
public class Status
{
public ObservableCollection<Digital> Digital {get;set;}
public ObservableCollection<Monitoring> Monitoring{get;set;}
etc....
}
public class Digital
{
// I'll replace the # in the JSON with "number" or something
// before deserializing
public string # {get;set;}
public string Desc {get;set;}
public string Ss {get;set;}
public string Dt {get;set;}
public string InAlarm {get;set;}
}
然后我想通过做这样的事情来访问嵌套对象。在这个例子中,我想要“D” JSON 类型,以及“IO”#1 的数据:
var _statusObjMagic = new Status();
// Go get the JSON and deserialize it.. build up the _statusObjMagic
Some magical code here...
// Get some data from the JSON response
string inputOneDescription = _statusObjMagic.Digital[0].Desc;
反序列化此 JSON 并获得上述结果的最简单方法是什么?也许我正在解决这个问题,并且有一种更简单的方法可以反序列化 JSON 并访问所有数据。我完全开放。
【问题讨论】:
-
你在创建那个 JSON 吗?如果不是,它来自哪里?虽然 JSON 在技术上是有效的,但它没有任何意义