JmingL
JmingL

Reputation: 21

Retrieve JSON data in Xamarin iOS

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,

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?

Upvotes: 0

Views: 2297

Answers (3)

user6919165
user6919165

Reputation:

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)
                {

                }

Upvotes: 0

Barletta
Barletta

Reputation: 224

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.

Upvotes: 1

Yahya
Yahya

Reputation: 3444

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;

Upvotes: 1

Related Questions