反序列化json时得到null

时间:2015-06-01 06:21:06

标签: c# json serialization

我正在尝试将我的Web响应json反序列化为一个对象,在反序列化后获取null时的对象。这是我的代码:

JSON回复:

{
  "@odata.context": "https://outlook.office365.com/api/v1.0/$metadata#Me/CalendarView",
  "value": [
    {
      "@odata.id": "https://outlook.office365.com/api/v1.0/Users('sathesh@newtechsolution.onmicrosoft.com')/Events('AAMkADQzMGVmNjZmLWY1YjAtNGFkYS1hODY0LTdiMWZlZjZjYmIwOABGAAAAAAAaluoeH9c2Qq33MvKTCqzgBwD1QTj3IO57QaWZ9MZF6weaAAAAAAENAAD1QTj3IO57QaWZ9MZF6weaAAAAAA0kAAA=')",
      "@odata.etag": "W/\"9UE49yDue0GlmfTGResHmgAAAAANag==\"",
      "Id": "AAMkADQzMGVmNjZmLWY1YjAtNGFkYS1hODY0LTdiMWZlZjZjYmIwOABGAAAAAAAaluoeH9c2Qq33MvKTCqzgBwD1QTj3IO57QaWZ9MZF6weaAAAAAAENAAD1QTj3IO57QaWZ9MZF6weaAAAAAA0kAAA=",
      "ChangeKey": "9UE49yDue0GlmfTGResHmgAAAAANag==",
      "Categories": [],
      "DateTimeCreated": "2015-05-20T12:03:09.4043813Z",
      "DateTimeLastModified": "2015-05-20T12:03:09.5606394Z",
      "Subject": "Interview Sample",
      "BodyPreview": "Interview For API discussion.",
      "Body": {
        "ContentType": "HTML",
        "Content": "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<style type=\"text/css\" style=\"display:none;\"><!-- P {margin-top:0;margin-bottom:0;} --></style>\r\n</head>\r\n<body dir=\"ltr\">\r\n<div id=\"divtagdefaultwrapper\" style=\"font-size:12pt;color:#000000;background-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;\">\r\n<p>Interview For API discussion.<br>\r\n</p>\r\n</div>\r\n</body>\r\n</html>\r\n"
      },
      "Importance": "Normal",
      "HasAttachments": false,
      "Start": "2015-05-20T16:00:00Z",
      "StartTimeZone": "Sri Lanka Standard Time",
      "End": "2015-05-20T17:00:00Z",
      "EndTimeZone": "Sri Lanka Standard Time",
      "Reminder": 15,
      "Location": {
        "DisplayName": "Interview Sample  Chennai MRC NAGAR",
        "Address": { "Street": "", "City": "", "State": "", "CountryOrRegion": "", "PostalCode": "" },
        "Coordinates": { "Accuracy": "NaN", "Altitude": "NaN", "AltitudeAccuracy": "NaN", "Latitude": "NaN", "Longitude": "NaN" }
      },
      "ResponseStatus": { "Response": "Organizer", "Time": "0001-01-01T00:00:00Z" },
      "ShowAs": "Busy",
      "IsAllDay": false,
      "IsCancelled": false,
      "IsOrganizer": true,
      "ResponseRequested": true,
      "Type": "SingleInstance",
      "SeriesMasterId": null,
      "Attendees": [
        {
          "EmailAddress": { "Address": "skumar@viswambara.com", "Name": "skumar@viswambara.com" },
          "Status": { "Response": "None", "Time": "0001-01-01T00:00:00Z" },
          "Type": "Required"
        }
      ],
      "Recurrence": null,
      "Organizer": {
        "EmailAddress": { "Address": "sathesh@newtechsolution.onmicrosoft.com", "Name": "sathesh kumar" }
      },
      "iCalUId": "040000008200E00074C5B7101A82E0080000000019D340F0F492D0010000000000000000100000005BA1B6261EECD34D991C5BE7D4A70547"
    }
  ]
}

类别:

public class value
{
    public string Id { get; set; }
    public string ChangeKey { get; set; }
    public List<object> Categories { get; set; }
    public string DateTimeCreated { get; set; }
    public string DateTimeLastModified { get; set; }
    public string Subject { get; set; }
    public string BodyPreview { get; set; }
    public Body Body { get; set; }
    public string Importance { get; set; }
    public bool HasAttachments { get; set; }
    public string Start { get; set; }
    public string StartTimeZone { get; set; }
    public string End { get; set; }
    public string EndTimeZone { get; set; }
    public int Reminder { get; set; }
    public Location Location { get; set; }
    public ResponseStatus ResponseStatus { get; set; }
    public string ShowAs { get; set; }
    public bool IsAllDay { get; set; }
    public bool IsCancelled { get; set; }
    public bool IsOrganizer { get; set; }
    public bool ResponseRequested { get; set; }
    public string Type { get; set; }
    public object SeriesMasterId { get; set; }
    public List<Attendee> Attendees { get; set; }
    public object Recurrence { get; set; }
    public Organizer Organizer { get; set; }
    public string iCalUId { get; set; }
}

反序列化:

JavaScriptSerializer json = new JavaScriptSerializer();
value condiiton = (value)json.Deserialize(responcedata, typeof(value));

1 个答案:

答案 0 :(得分:0)

让我们看看你的JSON对象。实际上你有一个匿名的JSON(让它称之为response)对象有两个对象:string&#39; @ odata.context&#39;和数组&#39;值&#39; value C#类类型的对象。

您已经描述了value类,但现在您正在尝试将response对象反序列化为value,这是不正确的。
您需要描述相应的类并反序列化它。

public class Response 
{
    public value[] value { get; set; }
}

然后你可以这样做:

JavaScriptSerializer json = new JavaScriptSerializer();
Response response = (Response)json.Deserialize(responcedata, typeof(Response));