I am trying to get thumb image urls in JSON below. What I have below works well if the "image" is not null and if "thumb" is not null. But if either is null, I get NullException.
Here is example of simplified JSON:
{
"total_items": "24",
"page_number": "1",
"page_size": "10",
"page_count": "3",
"cars": {
"car": [
{
"image": {
"thumb": {
"width": "32",
"url": "<image_url>/honda1.jpg",
"height": "32"
}
}
},
{
"image": null,
}
]
}
}
and here is how I deserialize it using Newtonsoft.JSon:
dynamic data = (JObject)JsonConvert.DeserializeObject(json);
foreach (dynamic d in data.cars.car)
{
Car c = new Car();
c.ThumbUrl = d.image.thumb.url; //image AND thumb could be null
}
I have read few posts on how to handle this and tried adding [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] in my Car class above the Url property but that did not solve anything.
Thanks,