0

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?

JmingL
  • 21
  • 6

3 Answers3

1

Use the returned structure and put it on something like http://json2csharp.com/ to get a mapping object. And then you can deserialize it ref:

RootObject obj = new JavaScriptSerializer().Deserialize<RootObject>(response.Content);
//obj.YourDesiredProperty;
Community
  • 1
  • 1
Yahya
  • 3,386
  • 3
  • 22
  • 40
  • my obj."Property" returns empty in Console.WriteLine. Still cant manage to get the data from API – JmingL May 03 '17 at 01:35
  • Hi Yahya, Currently I having trouble to include System.Web.Extensions in my project now. I am using Visual Studio for Mac which is based on Xamarin iOS – JmingL May 03 '17 at 01:52
  • Use this: curTest = JsonConvert.DeserializeObject>(json1); because you are parsing a list of objects. – Yahya May 03 '17 at 08:19
1

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?

It depends on the way which the api was designed. When you say parameters, are you trying to say routes or end-points? Your question is a little bit confused.

How do I retrieve only specific data from the API?

Can you be more specific? Are you talking about the route which you are trying to retrieve your data?

If the Api consists of an Object in other part, how to do get the data in the nested object?

You can use Newtonsoft.Json(which is the most popular package in nuget) to deserialize your json object. Also, it will only be possible if you create an C# class which will be a mirror from your json model.

You can use an online tool like https://jsonutils.com/ to create this class automatically.

edit:

Since you are trying to deserialize a list of several objetcs, you should try the following approach:

var json1 =
            "[{\"id\":\"518523721\",\"name\":\"ftyft\"},\r\n{\"id\":\"527032438\",\"name\":\"ftyftyf\"},\r\n{\"id\":\"527572047\",\"name\":\"ftgft\"}, \r\n{\"id\":\"531141884\",\"name\":\"ftftft\"}]";

var curTest = JsonConvert.DeserializeObject<List<TestInfo>>(json1);
Console.WriteLine(curTest[0].id);

It will works now.

Barletta
  • 224
  • 1
  • 5
0

I was little late but it works for me.

var request = HttpWebRequest.Create(@"http://peerff.azurewebsites.net/api/team");
                request.ContentType = "application/json";
                request.Method = "GET";

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {

                }
  • Thanks Kaan though its a little late. I managed to complete the application already. Once again, thanks for your reply – JmingL Aug 29 '17 at 08:24