简体   繁体   中英

getting null while deserialization json

I am trying to deserialize my web response json to an object, at the time object getting null after deserialization. This is my code:

JSON response:

{
  "@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"
    }
  ]
}

Class:

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; }
}

Deserialization:

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

Let's look at your JSON object. You actually have an anonymous JSON (let's call it response ) object with two objects: string '@odata.context' and array 'value' of objects of value C# class type.

You have described value class but now you are trying do deserialize response object to value which is incorrect.
You need to describe the corresponding class and deserialize it.

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

Then you can do it this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM