I have trouble in retrieve data from Json Object from Restful. Currently, I am using RestSharp to get the response from the api. Based on what I have studied in Use C# to get JSON Data
I do not quite understand on the data retrieving. If I want to retrieve just a single data from the API,
- Can I declared on specific data that I want to retrieve from the api in Model class or I need to declare every parameters from the API?
- How do I retrieve only specific data from the API?
- If the Api consists of an Object in outter part, how to do get the data in the nested object?
Please enlighten me on this matter. Thank you in advanced.
This is the sample code that I created,
var client = new RestClient(<myAPIkey>);
var request = new RestRequest(String.Format("post", Method.GET));
client.ExecuteAsync(request, response =>
{
Console.WriteLine(response.Content);
});
EDITED
I have edited my project as I am testing to get data from hardcoded Json data as such,
[{"id":"518523721","name":"ftyft"},
{"id":"527032438","name":"ftyftyf"},
{"id":"527572047","name":"ftgft"},
{"id":"531141884","name":"ftftft"}]
With Model class of,
public class TestInfo
{
public string id { get; set; }
public string name { get; set; }
}
and the code for deserialize,
TestInfo curTest = new TestInfo();
curTest = JsonConvert.DeserializeObject<TestInfo>(json1);
Console.WriteLine(curTest.id);
I still failed to get the id and name from the json data as it returns empty in Console.WriteLine. Can you please guide me through on how to read the json data?
>(json1); because you are parsing a list of objects.
– Yahya May 03 '17 at 08:19