1

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; }
}
Community
  • 1
  • 1
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • Is using `Json.NET` (which can handle this) rather than the default `JavaScriptSerializer` an option? –  Jun 24 '16 at 05:41
  • `DeliveryStatus` class has few properties instead of one. There are: message, code , description, session-seconds. – Maciej Los Jun 24 '16 at 05:41
  • Yes @StephenMuecke.. Please let me know how I can achieve it with `Json.NET`? – Guruprasad J Rao Jun 24 '16 at 05:43
  • `DataContractJsonSerializer` can also be used as per [this answer](http://stackoverflow.com/questions/14753113/how-to-deserialize-a-property-with-a-dash-in-its-name-with-newtonsoft-jso) –  Jun 24 '16 at 05:43
  • @MaciejLos.. Yea agreed.. But in my case, its not necessary.. – Guruprasad J Rao Jun 24 '16 at 05:43
  • Isn't better to define these fields and tell to deserializer to ignore them? – Maciej Los Jun 24 '16 at 05:46
  • 2
    For `Json.NET`, refer [this answer](http://stackoverflow.com/questions/21563702/model-to-match-this-json-for-deserialization-field-names-with-dashes) –  Jun 24 '16 at 05:46
  • @StephenMuecke.. I tried with `[JsonProperty(PropertyName = "delivery-status")]` but did not help. do I need to deserialize in a separate way? – Guruprasad J Rao Jun 24 '16 at 05:46
  • 1
    That should work if you change the property name to (say) `DeliveryStatus` –  Jun 24 '16 at 05:47
  • @MaciejLos.. I believe, it does automatically if am not wrong.. – Guruprasad J Rao Jun 24 '16 at 05:47
  • @StephenMuecke Updated question.. Did not work either.. Is deserializing way proper in my case? – Guruprasad J Rao Jun 24 '16 at 05:50
  • @StephenMuecke.. Any help here? – Guruprasad J Rao Jun 24 '16 at 06:08
  • 1
    I assume you did set the serializer to use Json.NET. Cant see any reason why it would not work, but I can't test it at the moment (will check over the weekend if you have not resolved it) –  Jun 24 '16 at 06:11
  • @StephenMuecke Sure.. Let me know if I can provide you `json` sample through any other medium.. In the mean time am opting with `dynamic` declaration, even though I feel its not proper.. – Guruprasad J Rao Jun 24 '16 at 06:12
  • @StephenMuecke you are right (of course), it does work. See my answer. – Patrick Hofman Jun 24 '16 at 06:30

1 Answers1

2

I am not sure what the issue is at your end, but at least it works if you use this code. Make sure to include a recent version of Newtonsoft.Json in your project and you should be fine.

public class DeliveryStatus
{
    public object message { get; set; }
    public int code { get; set; }
    public string description { get; set; }
    [JsonProperty("session-seconds")]
    public int session_seconds { get; set; }
}

public class Item
{
    [JsonProperty("delivery-status")]
    public DeliveryStatus delivery_status { get; set; }
    public string @event { get; set; }
    [JsonProperty("log-level")]
    public string log_level { get; set; }
    public string recipient { get; set; }
}

public class RootObject
{
    public List<Item> items { get; set; }
}

public static void Main(string[] args)
{
        string json = @"{
  ""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""
  }]
}";

    RootObject r = JsonConvert.DeserializeObject<RootObject>(json);
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I don't know what was wrong, but I changed this line `(test)json.Deserialize(resp.Content, typeof(test));` to `test r = JsonConvert.DeserializeObject(resp.Content);` and yes now it works.. So basically, the `deserialization` was not happening properly with `json.Deserialize`? – Guruprasad J Rao Jun 24 '16 at 07:54
  • I guess you were trying to mix Microsoft's `JavaScriptSerializer` with JSON.NET's `JsonProperty` attributes. That won't work. – Patrick Hofman Jun 24 '16 at 08:11
  • Yea.. That's the problem may be.. Anyways.. Nice to know the solution.. Thanks much and thank you @StephenMuecke.. Finally its resolved.. :) – Guruprasad J Rao Jun 24 '16 at 08:21