Hello friends. In this article, I will talk about another example of a subject that I received a lot of mail. Json take data. I will not mention how important it is to pull Json data, or why we need to pull Json data. You can reach my previous article here.
A friend asked us if you can share the URL by taking a sample of the data here. I’ll do an example of this URL.
We go to the URL and paste all the data here. This address turns our json data into a model. We include this model in our project.
using System; namespace XamTakeJson.Model { public class SampleModel { public string id { get; set; } public string baslik { get; set; } public string detay { get; set; } public object ustu { get; set; } public string url { get; set; } public object gruplar { get; set; } public string resim { get; set; } public string dil { get; set; } } }
Then we need to pull the data from the URL and convert it into a model in our project. Of course we need to write a service for this. We need to install the Newtonsoft.Json package for us to use in the service. We need to specify our URL first within the service. After specifying the url, we must create an HttpClient. We can retrieve data from a Url with this client. Of course we need to tell the client that we created because we will pull the json data. Then we just have to write the data extraction function. This function should return my models that come back in the form of a list and must be asynchronous because we will pull the data from the url. Then we can start making transactions. We need to tell the client that we created read the data as a string at the address we specified. So we’re saying that everything in the url is fetched to me as a string and I assign it to a variable named result. Then I deserialize with the help of my Newtonsoft.Json package that I have installed and turn it into a list model. Actually, it’s that simple.
using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json; namespace XamTakeJson.Providers { public class ServiceManager { private static string Url = ""; private HttpClient GetClient() { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Accept", "application/json"); return client; } public async Task<List<Model.SampleModel>> Get() { using (HttpClient client = GetClient()) { var result = await client.GetStringAsync(Url); return JsonConvert.DeserializeObject<List<Model.SampleModel>>(result); } } } }
After typing the service, we just need to use this back. I’m using it on a Xamarin project. Everything that’s already been done here is common. So you can use it in all your projects. You create a ServiceManager in a service call, then call the function I created with this manager and assign it to a variable.
ServiceManager manager = new ServiceManager(); var takenData = manager.Get();
When I look at my data, I see that there are 22.
When I examine any object in the list, I see that the data is coming correctly.
If you have questions, you can reach us by e-mail or comment. Good work.
Leave a Reply