Bokbob
Bokbob

Reputation: 89

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

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": "[email protected]",
    "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
      }
    ]
  }]}}

Upvotes: 1

Views: 3280

Answers (1)

H77
H77

Reputation: 5967

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

Upvotes: 3

Related Questions