Json Response Help (Beginner)

144 views
Skip to first unread message

Zaeem Amjad

unread,
Jul 19, 2019, 2:36:53 PM7/19/19
to RestSharp
I want to parse the following Json response:
{
  "message": "string",
  "errorCode": "string",
  "totalSize": 0,
  "offset": 0,
  "done": true,
  "nextRecordsUrl": "string",
  "records": [
    {
      "id": "string",
      "name": "string",
      "external_ID_vod__c": "string",
      "vExternal_Id_vod__c": "string"
    }
  ]
}
**Note that there are multiple 'record'

The class I am using to deserialize the Json response is:
public class Products
            {
                public string message { get; set; }
                public string errorCode { get; set; }
                public int totalSize { get; set; }
                public int offset { get; set; }
                public bool done { get; set; }
                public string nextRecordsUrl { get; set; }
                public List<RecordProduct> records { get; set; }

            }

public class RecordProduct
        {
            public string id { get; set; }
            public string name { get; set; }
            public string external_ID_vod__c { get; set; }
            public string vExternal_Id_vod__c { get; set; }
        }


The code that is calling the API:
public void getAllProductInfo()
        {
            RestClient client = new RestClient("blahblahblah/");

            RestRequest request = new RestRequest("/api/Products", Method.GET);

            string securityToken = getBearerToken();
            request.AddHeader("Accept", "application/json");
            request.AddHeader("Authorization", "Bearer " + securityToken);

            IRestResponse<JsonResponseSetup.Datum.Products> response = client.Execute<JsonResponseSetup.Datum.Products>(request);

            var res = response.Data.records;
            res.ForEach(Console.WriteLine);

         }


When I execute the last line: res.ForEach(Console.WriteLine); I do not get the records to print. 

dharmesh sharma

unread,
Jul 20, 2019, 5:12:44 PM7/20/19
to rest...@googlegroups.com
Hii,

Do one thing parse by newtonsoft json lib its easy to parse json to class .

Thanks
DHARMESH 

--
You received this message because you are subscribed to the Google Groups "RestSharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to restsharp+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/restsharp/f081e568-2d2b-4fe0-943b-56d2fd6db766%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages