【问题标题】:Correct number of objects after JSON deserialization, but they are all blankJSON反序列化后对象数量正确,但都是空白
【发布时间】:2018-04-14 05:43:57
【问题描述】:

Send Grid 退回 API 会返回一个类似于此的列表:

"[
     {\"created\":1487173664,\"email\":\"lklklk@kkk.com\",\"reason\":\"550 No Such User Here \",\"status\":\"550\"}
    ,{\"created\":1487169530,\"email\":\"bb@hotmail.com\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"}
    ,{\"created\":1487095343,\"email\":\"lsdkjf@hotmail.com\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"}
    ,{\"created\":1487093087,\"email\":\"sldf@hotmail.com\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"}
    ,{\"created\":1487085008,\"email\":\"sdlkfj@hotmail.com\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"}
    ,{\"created\":1487082934,\"email\":\"mickey.mouse@disney.com\",\"reason\":\"550 Invalid recipient <mickey.mouse@disney.com> (#5.1.1) \",\"status\":\"550\"}
]"

使用以下类,我尝试反序列化该 JSON(尝试使用和不使用 [DataMember] 装饰,因为序列化可以正常工作):

[DataContract(Name = "SendGridBounce")]
public class SendGridBounce
{
    [DataMember]
    public int Created { get; set; }
    [DataMember]
    public string Email { get; set; }
    [DataMember]
    public string Reason { get; set; }
    [DataMember]
    public string Status { get; set; }
}

[CollectionDataContract(Name = "SendGridBounceList")]
public class SendGridBounceList : List<SendGridBounce>
{
}

我就是这样做的:

var client = new SendGridClient("some API key here");
string queryParams = String.Format(CultureInfo.InvariantCulture, "{{ 'end_time': {0},  'start_time': 1 }}", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
var response = Task.Run(() => client.RequestAsync(SendGridClient.Method.GET, urlPath: "suppression/bounces", queryParams: queryParams)).Result;

DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(SendGridBounceList));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(response.Body.ReadAsStringAsync().Result)))
{
    SendGridBounceList bl = js.ReadObject(ms) as SendGridBounceList;

    foreach (var b in bl)
    {
        tbxOutput.Text += b.Created.ToString() + ", " + b.Status + ", " + b.Email + ", " + b.Reason + Environment.NewLine;
    }
}

最终结果是列表中的项与 SendGrid 返回的 JSON 中的项一样多,但每个项都未初始化 {0, null, null, null}。我做错了什么?

【问题讨论】:

  • 这可能是您的属性名称的大小写。 JSON 中的属性是小写的。
  • @Amy 没有区别
  • @L.B 你能再详细一点吗?

标签: c# json deserialization .net-4.6.2


【解决方案1】:

解决方案是更改DataMember 装饰如下:

[DataContract(Name = "SendGridBounce")]
public class SendGridBounce
{
    [DataMember(Name ="created")]
    public int Created { get; set; }
    [DataMember(Name = "email")]
    public string Email { get; set; }
    [DataMember(Name = "reason")]
    public string Reason { get; set; }
    [DataMember(Name = "status")]
    public string Status { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-23
    • 2021-01-16
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多