Newtonsoft.Json - Json.Net反序列化

时间:2014-04-08 14:11:32

标签: c# json json.net

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

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

基本上,JSON被" Type"分开。 (" D"," M"," O"," R"," G"或&# 34; 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的对象,它将包含每个" Type"的字段。 (D,M,O,R,G或A)。请参阅下面的示例类,其中" D" type是一个Digital类和" M" type是一个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;}
}

然后,我希望通过执行类似的操作来访问嵌套对象。在这个例子中,我想要&#34; D&#34; JSON类型,&#34; IO&#34;的数据#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并访问所有数据。我完全开放了。

1 个答案:

答案 0 :(得分:3)

您需要有一个适当的类来反序列化。

我使用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 Property Attribute告诉Json.NET如何转换它。

然后你可以反序列化。

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