I have the following data:
{"data":{"id":"7IaWnXo","title":null,"description":null,"datetime":1397926970,"type":"image/png","animated":false,"width":60,"height":60,"size":1277,"views":0,"bandwidth":0,"favorite":false,"nsfw":null,"section":null,"deletehash":"KYIfVnHIWWTPifh","link":"http://i.imgur.com/7IaWnXo.png"},"success":true,"status":200}
and I'm trying to serializer it into this:
public struct ImageInfoContainer
{
public ImageInfo data {get; set;}
bool success { get; set; }
string status { get; set; }
}
public struct ImageInfo
{
public string id {get; set;}
public string title { get; set; }
public string url { get; set; }
public string description {get; set;}
public string datetime {get; set;}
public string type {get; set;}
public string animated {get; set;}
public int width {get; set;}
public int height {get; set;}
public int size {get; set;}
public int views {get; set;}
public int bandwidth {get; set;}
public bool favourite {get; set;}
public bool nsfw {get; set;}
public string section {get; set;}
public string deletehash {get; set;}
public string link {get; set;}
}
and I'm getting:
An exception of type 'System.InvalidOperationException' occurred in System.Web.Extensions.dll but was not handled in user code
Additional information: Cannot convert null to a value type.
What am I doing wrong?
In your JSON data: nsfw is null.
But in ImageInfo struct, nsfw is defined as a boolean (it can't be null, only true or false)
You have 2 possibilities.
null for nsfw. public bool? nsfw {get; set;} If you take second option, this will allow you to have true, false or null as a value for nsfw and you won't have this error anymore.
Both Nullable<bool> and bool? are the same syntax.
More info about Nullable Types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With