【发布时间】:2019-09-21 03:53:27
【问题描述】:
我从一些服务器收到 json 响应,格式如下:
[
{
"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"
}]
在 mvc 控制器操作中,我通过以下模型获得这些响应:
[ValidateInput(false)]
[HttpPost]
public async Task<ActionResult> Index(ResponseModel[] rec)
{
}
我的模型是这样的:
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; }
}
实际上,我正在初始化所有属性,但不是 smtp-id。所以请建议我如何将响应“smtp-id”属性映射到我的模型。
【问题讨论】:
-
不清楚您在问什么以及您尝试过什么。请检查stackoverflow.com/help/how-to-ask 并考虑改进您的问题。
-
嗨,我想通过模型(响应模型)在控制器中获取 json 数据(如上所示的格式)。我已经展示了我正在尝试并正确获取除“smtp_id”之外的所有其他字段的值,因为json返回“smtp-id”(带连字符),但mvc模型不允许连字符,所以我使用下划线作为(“smtp_id ”)。但它没有被映射。那我应该用什么?
标签: asp.net-mvc