Hi
How to retreive Token value i.e data from the below response.
C#
{"status":true,"data":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2NzE1MTcxOTEsImp0aSI6IlM3OXNtbzlSWGJVdDlUaVJHT2RhRWJGVFBVbGFGclJaSTJ3WnMzNGpsRk09IiwibmJmIjoxNjcxNTE3MTkxLCJleHAiOjE2NzE1Mjc5OTEsImRhdGEiOnsidXNlcl9pZCI6IjQ3MDYyIiwicGFyZW50X2lkIjoiMCJ9fQ.xslY3b3XLJcoOJJ7SxISYCRg-k4bINrshpH6YLJvGfP_7bO4HCg1ccVkbkf7vE2895dmO2B3BQ9ATMFoXOMuXw"}Thanks

Deepak TewatiaRank #142 · 16K reputationPosted Dec 20, 2022, 1:47 AM EST
Hi Ramco,
We have two solution to solve this problem:
Solution-1
Parse the response string into a JSON object using a JSON parsing library such as Newtonsoft.Json. You can do this by calling the JsonConvert.DeserializeObject method and passing in the response string as the argument. This will give you a dynamic object that you can access the properties of.
Now access the "data" property of the dynamic object using dot notation.
dynamic json = JsonConvert.DeserializeObject(response); string token = json.data;Now, You can now use the token variable to access the token value.
Solution-2
Define a class to hold the data in the response, and use a JSON deserializer to deserialize the response string into an instance of that class. This can be more efficient and easier to maintain than using a dynamic object.
//Model Clss public class ResponseData { public bool Status { get; set; } public string Data { get; set; } } // Controller ResponseData data = JsonConvert.DeserializeObject(response); string token = data.Data;Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Dec 20, 2022, 1:32 AM EST
IRestResponse response = client.Execute(request); var deserializedObject = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); Console.WriteLine(deserializedObject.data); /// class public class MyClass { public bool status {get;set;} public string data {get;set;} }