RestSharp:从result.Data获取null,在调试结果时返回JSON

时间:2016-05-30 06:29:28

标签: c# json api deserialization restsharp

我正在使用RestSharp并尝试反序列化JSON结果集,当我运行代码时,我从result.Data获取空值,尽管在调试时结果已返回JSON。

private static void GetPanelList()
{
    var client = new RestClient("http://CCCCCC.XXXXXX.com/API/v3/mailinglists/ML_dfgfghfghfh/contacts");
    var request = new RestRequest(Method.GET);
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("x-api-token", "JHFKFKJFYILIOROIY");
    // IRestResponse response = client.Execute(request);
    var result = client.Execute<List<PanelList>>(request);
    foreach(var i in result.Data)
    {
        foreach(var j in i.elements)
        {
            Console.WriteLine(j.firstName);
        }
    }

这是我的POCO:

public class PanelList
{
    public List<elements> elements { get; set; }
}

public class elements
{
    public string id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
    public string externalDataReference { get; set; }
    public List<EmbededData> ED { get; set; }
    public List<ResponseHistory> RH { get; set; }
    public List<EmailHistory> EH { get; set; }
}

以下是我试图解析的json结果的样子。

   {  "result": {
"elements": [
  {
    "id": "MLRP_b7q690QRSqCxVuR",
    "firstName": "S1-User1-firstname",
    "lastName": "S1-U2-lastname",
    "email": "S1U1@XXXX.org.au",
    "externalDataReference": null,
    "embeddedData": {
      "DateTaken": "20160519",
      "TriggerResponseID": "R_3fE6zgBzLa24dgD",
      "TriggerSurveyID": "SV_3TXTMnJlsUxVGRL"
    },
    "language": null,
    "unsubscribed": false,
    "responseHistory": [
      {
        "responseId": "R_3fE6zgBzLa24dgD",
        "surveyId": "SV_3TXTMnJlsUxVGRL",
        "date": "2016-05-20T04:09:09Z",
        "emailDistributionId": null,
        "finishedSurvey": true
      }
    ],
    "emailHistory": [
      {
        "emailDistributionId": "EMD_41wVLKlQaADQBcF",
        "date": "2016-05-24T00:33:02Z",
        "type": "Invite",
        "result": "Success",
        "surveyId": "SV_8wFieltINfFL2gl",
        "read": false
      }
    ]
  }]}}

1 个答案:

答案 0 :(得分:1)

你不应该读到一个结果&#34;容器&#34;对象

容器类:

public class PanelListContainer
{
    public PanelList result { get; set; }
}

获取代码:

var result = client.Execute<PanelListContainer>(request);