-2

I am getting json response from some server in format like:

[
    {
        "email":"john.doe@sendgrid.com",
        "sg_event_id":"VzcPxPv7SdWvUugt-xKymw",
        "sg_message_id":"142d9f3f351.7618.254f56.filter-147.22649.52A663508.0",
        "timestamp":"1386636112",
        "smtp-id":"<142d9f3f351.7618.254f56@sendgrid.com>",
        "event":"processed",
        "category":"category1",
        "id": "001",
        "purchase": "PO1452297845",
        "Segmentid": "123456"
    },
    {
        "email":"not an email address",
        "smtp-id":"<4FB29F5D.5080404@sendgrid.com>",
        "timestamp":"1386636115",
        "reason":"Invalid",
        "event":"dropped",
        "category":"category2",
        "id":"001",
        "purchase":"PO1452297845",
        "Segmentid":"123456"
    },
    {
        "email":"john.doe@sendgrid.com",
        "sg_event_id":"vZL1Dhx34srS-HkO-gTXBLg",
        "sg_message_id":"142d9f3f351.7618.254f56.filter-147.22649.52A663508.0",
        "timestamp":"1386636113",
        "smtp-id":"<142d9f3f351.7618.254f56@sendgrid.com>",
        "event":"delivered",
        "category":"category1",
        "id": "001",
        "ip": "174.56.33.234",
        "url": "http://www.google.com/",
        "purchase": "PO1452297845",
        "Segmentid":"123456"
    }]

In mvc controller action I am getting these response via model like:

[ValidateInput(false)]
        [HttpPost]
        public async Task<ActionResult> Index(ResponseModel[] rec)
        {
        }

My model is like:

public class ResponseModel
{
    public int ReportId { get; set; }
    public string raw { get; set; }
    public int event_post_timestamp { get; set; }
    public string SegmentId { get; set; }
    public string url { get; set; }
    public string type { get; set; }
    public string status { get; set; }
    public string attempt { get; set; }
    public string useragent { get; set; }
    public string ip { get; set; }
    public string reason { get; set; }
    public string response { get; set; }
    public string newsletter { get; set; }
    public string category { get; set; }
    public string sg_message_id { get; set; }
    public string sg_event_id { get; set; }
    [Column("smtp-id")]
    [DataMember(Name="smtp-id")]
    [JsonProperty("smtp-id")]
    public string smtp_id { get; set; }
    public string email { get; set; }
    [Column("event")]
    [JsonProperty("event")]
    [DataMember(Name = "event")]
    public string @event { get; set; }
    public int timestamp { get; set; }
}

In action I am getting all property initialized but not smtp-id. So please suggest me how can I map response "smtp-id" attribute to my model.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    It's not clear what you are asking and what you have tried. Please, check https://stackoverflow.com/help/how-to-ask and consider improving your question. – arghtype Jul 30 '14 at 09:54
  • hi arghtype, i want to get json data (format shown above) in controller via model (Response Model). I had shown what i am exactly trying and getting value of all other fields correctly except "smtp_id" as json is returning "smtp-id" (with hyphen), but mvc model doesn't allow hyphen so i used underscore as ("smtp_id"). but it is not getting mapped. So what should i use? – user3648466 Jul 30 '14 at 10:40

2 Answers2

1

Create your own ActionFilterAttribute similar to what was done here

Community
  • 1
  • 1
Andy Refuerzo
  • 3,312
  • 1
  • 31
  • 38
0

I know this is a really old post but I came across this and found that there are two things needed.

  1. [JsonProperty(PropertyName = "message-id")] using Newtonsoft.Json and
  2. a simple 'custom' model binder https://stackoverflow.com/a/34030497/2455159

Then any Json data object in can be bound to a model/ viewmodel in a controller accepting posted data like a webhook, that has invalid (at least a '-' ) character in property names/ attributes.

Stephan Luis
  • 911
  • 1
  • 9
  • 24