How to Deserialize JSON Response to Class with RestSharp?
The term Deserialization here means the conversion from the String form of JSON to a
Class form. This is also called Object Representation of structured data.
- Add Newtonsoft.json to the project via Nuget Package Manager => Manage Nuget packages for Solutions.
- Create a model class of the json response like this
public class Users { public string name { get; set; } public string job { get; set; } public string id { get; set; } public DateTime createdAt { get; set; } }
- Make appropriate changes in the code like this
public class DemoGetTest { private string BaseUrl = "https://reqres.in/"; [Test] public void DeserializingJsonResponse() { var restClient = new RestClient(BaseUrl); var restRequest = new RestRequest("/api/users?page=2", Method.GET); restRequest.AddHeader("Accept", "application/json"); IRestResponse response = restClient.Execute(restRequest); var content = response.Content; if (response.IsSuccessful) { Console.WriteLine("Status Code " + response.StatusCode); Console.WriteLine("Response Content " + response.Content); } //De-serialisation of Response Data Root json = JsonConvert.DeserializeObject<Users>(content);
if(json.data[0].first_name== "Michael") { Console.WriteLine("operation passed "); } var jsonobject=JObject.Parse(content); int pagevalue =(int)jsonobject.GetValue("page"); } }

















