将JSON和子对象反序列化为List <class>

时间:2017-01-25 14:57:23

标签: c# json deserialization

我在StackOverflow上查了几个问题,但找不到适合我的方法。

我基本上试图将以下JSON反序列化为我的“suppressContact”类的列表,但我无法让它工作。

JSON看起来像这样:

[
  {
    "suppressedContact": {
    "id": 23,
    "email": "nelson.redeker@example.com",
    "optInType": "Unknown",
    "emailType": "PlainText",
    "dataFields": null,
    "status": "Unsubscribed"
  },
  "dateRemoved": "2015-09-18T15:26:25.2612537Z",
  "reason": "Unsubscribed"
  },
  {
    "suppressedContact": {
      "id": 25,
      "email": "terry.mccarthy@example.com",
      "optInType": "VerifiedDouble",
      "emailType": "Html",
      "dataFields": null,
      "status": "Unsubscribed"
    },
    "dateRemoved": "2015-02-24T13:06:42.933Z",
    "reason": "Unsubscribed"
  },
]

我试图反序列化的课程看起来像这样:

public class SuppressedRoot
{
    public Suppressedcontact suppressedContact { get; set; }
    public DateTime dateRemoved { get; set; }
    public string reason { get; set; }
}

public class Suppressedcontact
{
    public int id { get; set; }
    public string email { get; set; }
    public string optInType { get; set; }
    public string emailType { get; set; }
    public object dataFields { get; set; }
    public string status { get; set; }
}

我正在使用这段代码来尝试实现这一目标:

List<Suppressedcontact> unsubscribedContacts = JsonConvert.DeserializeObject<List<Suppressedcontact>>(jsonResponse);

然而,这不起作用。

任何帮助都会受到赞赏,我正试图进入一个阶段,我可以遍历所有返回的联系人并提取电子邮件地址。

1 个答案:

答案 0 :(得分:0)

尝试替换

List<Suppressedcontact> unsubscribedContacts = JsonConvert.DeserializeObject<List<Supporesscontact>>(jsonResponse);

List<SuppressedRoot> unsubscribedContacts = JsonConvert.DeserializeObject<List<SuppressedRoot>>(jsonResponse);