{"id":1590,"date":"2019-06-04T20:59:40","date_gmt":"2019-06-04T20:59:40","guid":{"rendered":"https:\/\/sezeromer.com\/?p=1590"},"modified":"2023-02-27T22:06:21","modified_gmt":"2023-02-27T19:06:21","slug":"xamarin-ile-json-veri-cekmek","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/xamarin-ile-json-veri-cekmek\/","title":{"rendered":"Get JSON Data with Xamarin"},"content":{"rendered":"<p>Hello friends. I had written before about how to withdraw data from JSON, but there was a lot of mail and comment on this topic. That&#8217;s why I wanted to write a more detailed article, especially Xamarin. Actually, it has nothing to do with Xamarin. You can do the same for all .NET platforms.<\/p>\n<p>We need to have a service because we will pull JSON data. I wanted to use existing sample services instead of writing a service specific to this topic. In my previous articles, I mentioned how to write service. Search. I found a service that gives a list of Nobel Prize winners. We will make transactions through this service. You can reach <a href=\"http:\/\/api.nobelprize.org\/v1\/prize.json\">here<\/a>.<\/p>\n<p>First of all, in our project, we must have a class to send and receive data from a service. Here we create a HttpClient and do our transactions through this client. You can change this class according to the operations you want to do. You can add a token if you want. I&#8217;ve added it below to be an example. It pulls our data from this service class with GetAsync method. Of course, this function requires a return type and url as parameters.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">using Newtonsoft.Json;\r\nusing System;\r\nusing System.Net.Http;\r\nusing System.Net.Http.Headers;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace XamJson.Helpers\r\n{\r\n    class Service\r\n    {\r\n        public async Task&lt;TResult&gt; GetAsync&lt;TResult&gt;(string uri)\r\n        {\r\n            try\r\n            {\r\n                HttpClient httpClient = new HttpClient();\r\n                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(\"application\/json\"));\r\n                httpClient.DefaultRequestHeaders.Add(\"accept-language\", \"en\");\r\n                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(\"Bearer\", \"token\");\r\n\r\n                HttpResponseMessage response = await httpClient.GetAsync(uri);\r\n                string serialized = await response.Content.ReadAsStringAsync();\r\n                TResult result = JsonConvert.DeserializeObject&lt;TResult&gt;(serialized);\r\n                return result;\r\n            }\r\n            catch (Exception e)\r\n            {\r\n                App.Current.MainPage.DisplayAlert(\"Error\", \"General Error\", \"OK\");\r\n                return default(TResult);\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<\/div>\n<p>We have to build our models. There is a very useful site to create the model you can use here. When you copy all your data <a href=\"http:\/\/json2csharp.com\/\">here<\/a> and press the generate button, the models are returned to you.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1591\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/121212-1024x523.png\" sizes=\"(max-width: 640px) 100vw, 640px\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/121212-1024x523.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/121212-300x153.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/121212-768x392.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/121212.png 1920w\" alt=\"\" width=\"640\" height=\"327\" \/><\/p>\n<p>Let&#8217;s add this model to our project. When we generate the data, the model is as follows.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">using System.Collections.Generic;\r\n\r\nnamespace XamJson.Models\r\n{\r\n    public class Laureate\r\n    {\r\n        public string id { get; set; }\r\n        public string firstname { get; set; }\r\n        public string surname { get; set; }\r\n        public string motivation { get; set; }\r\n        public string share { get; set; }\r\n    }\r\n\r\n    public class Prize\r\n    {\r\n        public string year { get; set; }\r\n        public string category { get; set; }\r\n        public string overallMotivation { get; set; }\r\n        public List&lt;Laureate&gt; laureates { get; set; }\r\n    }\r\n\r\n    public class Nobel\r\n    {\r\n        public List&lt;Prize&gt; prizes { get; set; }\r\n    }\r\n}<\/pre>\n<\/div>\n<p>Then we need to do the necessary operations on the page we want to call the data. What we need to do here is to get an instance from the service class and run GetAsync from this service. You can see that the model works as a return type. The rest depends on how you use it. If you wish, you can show certain prizes on the screen or if you throw a ListView and bind all the data here, you can show all the data on the screen.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">using System;\r\nusing System.Collections.Generic;\r\nusing System.ComponentModel;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing Xamarin.Forms;\r\nusing XamJson.Helpers;\r\nusing XamJson.Models;\r\n\r\nnamespace XamJson\r\n{\r\n    [DesignTimeVisible(false)]\r\n    public partial class MainPage : ContentPage\r\n    {\r\n        Service service;\r\n        public MainPage()\r\n        {\r\n            InitializeComponent();\r\n            service = new Service();\r\n            GetNobelPrizes();\r\n        }\r\n\r\n        private async void GetNobelPrizes()\r\n        {\r\n            var takenData = await service.GetAsync&lt;Nobel&gt;(\"http:\/\/api.nobelprize.org\/v1\/prize.json\");\r\n        }\r\n    }\r\n}<\/pre>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1592\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/1-1024x576.png\" sizes=\"(max-width: 640px) 100vw, 640px\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/1-1024x576.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/1-300x169.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/1-768x432.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2019\/06\/1.png 1920w\" alt=\"\" width=\"640\" height=\"360\" \/><\/p>\n<p>In fact, these operations are so simple and easy. You can pull all kinds of data as you wish. If you have questions, you can reach us by e-mail or comment. Good work.<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello friends. I had written before about how to withdraw data from JSON, but there was a lot of mail and comment on this topic. That&#8217;s why I wanted to write a more detailed article, especially Xamarin. Actually, it has nothing to do with Xamarin. You can do the same for all .NET platforms. We [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1312,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[289,288,3],"tags":[613,496,399,614,616,8,7,343,615,345,395,440,497,22,119],"class_list":["post-1590","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-ios","category-xamarin-ile-yapilmis-projeler","tag-cekmek","tag-data","tag-json","tag-nobel","tag-odul","tag-omer","tag-omer-sezer","tag-parameter","tag-prize","tag-return","tag-service","tag-sezer","tag-veri","tag-xamarin","tag-xamarin-forms"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1590"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=1590"}],"version-history":[{"count":4,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1590\/revisions"}],"predecessor-version":[{"id":2631,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1590\/revisions\/2631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/1312"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=1590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=1590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=1590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}