我有一个动态的json数组数组,它本质上是动态的。我想反序列化为一个类。 "数据类型" tag决定C#class memeber的数据类型
[{
"model": "DeviceModel.DeviceInstance",
"name": "My-Device",
"variables": {
"Variable1": {
"SubVariable1": {
"DataType": "Double",
"Unit": "V",
"High": "3.5",
"Low": "3.2",
"Nominal": "3.3"
},
"SubVariable2": {
"DataType": "Double",
"Unit": "A",
"High": "10"
}
},
"Variable2": {
"DataType": "Int",
"Unit": "bytes",
"Max": "100000",
"Low": "10000",
"LowLow": "500"
}
},
"properties": {
"ConstantProperty": {
"PropertyName": {
"DataType": "String",
"Value": "12-34561"
}
}
}
}
]
答案 0 :(得分:1)
我已经解决了你的问题,发现下面的解决方案非常有效 参考: 1。http://json2csharp.com/#
2。JavaScriptSerializer.Deserialize array
public class SubVariable1
{
public string DataType { get; set; }
public string Unit { get; set; }
public string High { get; set; }
public string Low { get; set; }
public string Nominal { get; set; }
}
public class SubVariable2
{
public string DataType { get; set; }
public string Unit { get; set; }
public string High { get; set; }
}
public class Variable1
{
public SubVariable1 SubVariable1 { get; set; }
public SubVariable2 SubVariable2 { get; set; }
}
public class Variable2
{
public string DataType { get; set; }
public string Unit { get; set; }
public string Max { get; set; }
public string Low { get; set; }
public string LowLow { get; set; }
}
public class Variables
{
public Variable1 Variable1 { get; set; }
public Variable2 Variable2 { get; set; }
}
public class PropertyName
{
public string DataType { get; set; }
public string Value { get; set; }
}
public class ConstantProperty
{
public PropertyName PropertyName { get; set; }
}
public class Properties
{
public ConstantProperty ConstantProperty { get; set; }
}
public class RootObject
{
public string model { get; set; }
public string name { get; set; }
public Variables variables { get; set; }
public Properties properties { get; set; }
}
class Stackoverflow
{
static void Main(string[] args)
{
string strJSONData = "Your JSON Data";// Either read from string of file source
System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<RootObject> objRootObjectList = jsSerializer.Deserialize<List<RootObject>>(strJSONData);
Console.ReadLine();
}
}
我希望这会对你有所帮助。