RestSharp :Getting null from the result.Data althouh at the time of debug result has JSON returned

扶醉桌前 提交于 2019-12-11 05:56:19

问题


I am using RestSharp and trying to deserialize the JSON result set and when I run the code I'm getting null from the result.Data although at the time of debugging the result has JSON returned.

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);
        }
    }

Here is my 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; }
}

and here is how my json result look like that am trying to parse.

   {  "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:


Shouldn't you be reading into a result "container" object?

container class:

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

fetch code:

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


来源:https://stackoverflow.com/questions/37518988/restsharp-getting-null-from-the-result-data-althouh-at-the-time-of-debug-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!