I have below json
received from mailgun API
.
{
"items": [{
"delivery-status": {
"message": null,
"code": 605,
"description": "Not delivering to previously bounced address",
"session-seconds": 0
},
"event": "failed",
"log-level": "error",
"recipient": "test@test.com"
},
{
//some other properties of above types
}]
}
Now I was trying to create a class structure for above json
to auto-map the properties after deserializing
.
public class test
{
public List<Item> items { get; set; }
}
public class Item
{
public string recipient { get; set; }
public string @event { get; set; }
public DeliveryStatus delivery_status { get; set; }
}
public class DeliveryStatus
{
public string description { get; set; }
}
This is how I deserialize
and try to map the properties.
var resp = client.Execute(request);
var json = new JavaScriptSerializer();
var content = json.Deserialize<Dictionary<string, object>>(resp.Content);
test testContent = (test)json.Deserialize(resp.Content, typeof(test));
var eventType = testContent.items[0].@event;
var desc = testContent.items[0].delivery_status.description; //stays null
Now in the above class Item
, recipient
and @event
gets mapped properly and since it was a keyword
I was suppose to use preceding @
character and it works well. But the delivery-status
property from json
, does not get mapped with delevery_status
property in class DeliveryStatus
. I have tried creating it as deliveryStatus
or @deliver-status
. The earlier on doesn't map again and the later one throws compile time exception. Is there anyway these things can be handled, like declaring a property with -
in between? I cannot change response json
as it is not getting generated from my end. Hoping for some help.
Update
Changed the class as below referring this answer
, but did not help. Its null
again.
public class Item
{
public string @event { get; set; }
[JsonProperty(PropertyName = "delivery-status")]
public DeliveryStatus deliveryStatus { get; set; }
}