I'm working on a Xamarin Project. Basically, I want to receive data from the API and display it. I'm using RestSharp for this.
Here is my code for the API request.
string test;
test = "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq";
string s = string.Format("http://192.168.1.4:3116/api/user/getuser/{0}", test);
client = new RestClient(s);
request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
IRestResponse response2 = client.Execute(request);
This is the JSON object I receive.
{
"id": 1,
"token": "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq",
"email": "some email",
"password": "testpassword",
"currentCompany": "some company",
"currentRole": "Software Developer",
"date": "Something",
"name": "Some name",
"lastName": "Some surname",
"headLine": "Some text",
"education": "University of Hotshots",
"country": "Who cares",
"imageLocation": "Some url"
}
This is the class I created for it using the website: json2csharp.com/
class User
{
public int Id { get; set; }
public string Token { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string CurrentCompany { get; set; }
public string CurrentRole { get; set; }
public string Date { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public string HeadLine { get; set; }
public string Education { get; set; }
public string Country { get; set; }
public string ImageLocation { get; set; }
}
How I can change my code such that I can deserialize the response to an instance of the above class, so that I can use it to process the data? I read the posts here and tried the solutions, but they didn't seem to be working for me. So, I posted this question.
Using an array is an option as well; I can work with it. For reference, see PHP's $array = json_decode($somepostrequest, true), which turns the JSON object into an associative array. You can just call the object's $array['email'].