Newtonsoft.Json--Json.Net反序列化



首先,我对Json.Net没有太多经验,所以我提前道歉。我可以很好地反序列化基本对象,但这里是我遇到问题的地方。。。

我有一个疯狂的返回JSON,我不知道如何反序列化。我把它贴在下面了。我使用C#和Newtonsoft.Json(Json.Net)NuGet包来编写一个应用程序来使用这个Json。

基本上,JSON按"类型"("D"、"M"、"O"、"R"、"G"或"A")划分,该类型有一个嵌套的JSON对象("IO"),其中包含一组关于类型状态的相关信息。IO数据是一个列表。

[
   {
      "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"类型是Digital类,"M"类型是Monitoring类。

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 2 C#为您生成了一个。

public class IO
{
    [JsonProperty(PropertyName = "#")]
    public int Id{ get; set; }
    public string Desc { get; set; }
    public int SC { get; set; }
    public string DT { get; set; }
    public object InAlarm { get; set; }
    public string VS { get; set; }
}
public class RootObject
{
    public string Type { get; set; }
    public List<IO> IO { get; set; }
}

在C#中,#不是有效的属性名称。因此,我使用JSON.NET提供的JSON属性来告诉JSON.NET如何转换它

然后可以反序列化。

List<RootObject> mylist = JsonConvert.DeserializeObject<List<RootObject>>(json);

相关内容

  • 没有找到相关文章

最新更新