Press ESC to close

Taking Json Data

Hi friends, in this writing, I will talk about how to draw Json Data. First of all, Jason, we need to know why. Json is actually a data format created for JavaScript. Prior to Json, methods such as xml were used, but over time, these data types became heavy and inconvenient, along with Json data types. Json is short. The long form is JavaScript Object Notation, which means JavaScript object representation.

If we say where and how to use this json data, this is the answer; you will use it everywhere. For example, you are writing a project. In this project, we have both the web side and the mobile side as well as the Windows Form. You will work with the data in the project you are developing. One of the easiest ways to show and transfer this data is to work with Json. Of course, you also need to communicate with a web service. In my previous article, I talked about web service development with Asp.Net Core. Here too, the data returned in Json data format. You can find that article here.

In this article, I will talk about how you can project the data returned by this web service in this Json data type in any application. In case I am an example, I will draw data from the Console. Of course this should be an example for this. I will use Json data here as an example. But you can also use the data that you created yourself.

We are opening our console project. The first thing we do here is to load a package to make it easier to do transactions with Json data. This package is Newtonsoft.Json package.

We will receive the data after adding the package, but once we receive this data, we need to associate it with a type, a model. We need to create a model for this from the future model. As an example we have a very easy method to use if you say how to create for the data on the site. We will copy all the text on the site, and you can create a model for any data string from another nice site, Json2csharp. The model for the site where I left the link is like below.

public class OrnekVeri
    {

        public int userId { get; set; }
        public int id { get; set; }
        public string title { get; set; }
        public string body { get; set; }
    }

After he wrote a model for the future, he got the model to work. We need to write an administrator for this. After creating a class we need to create HttpClient. After creating this client we need to specify what type we will work with. We specify this because we will work with json data. Now we need to tell in which Url we should not withdraw the data. After you specify the client, we read the data in this field as a string. After reading this data, we integrate the data properly into our model with the package that we added first. As a last step, I return this data in the form of a list. What we need to be aware of here is that functions are asynchronous. On this count, we can do it in the back of the process and expect to have to wait for the data to be pulled.

private string Url = "https://jsonplaceholder.typicode.com/posts";
        private async Task<HttpClient> ClientOlustur()
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("Accept", "application/json");


            return client;
        }
        public async Task<IEnumerable<VeriCekmek.Models.OrnekVeri>> VerileriGetir()
        {
            HttpClient client = await ClientOlustur();
            var result = await client.GetStringAsync(Url);
            return JsonConvert.DeserializeObject<IEnumerable<OrnekVeri>>(result);

        }

Finally, we have not shown this data on the Console. After you take the data, you can easily display it. You can see how many pieces of data are in the photo and a sample data.

If you have any questions, please feel free to mail or comment me.

Comments (10)

Leave a Reply

Your email address will not be published. Required fields are marked *