﻿
{"id":2344,"date":"2013-11-19T22:08:19","date_gmt":"2013-11-19T22:08:19","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=2344"},"modified":"2015-09-09T22:25:34","modified_gmt":"2015-09-09T22:25:34","slug":"asp-net-mvc-json","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-mvc-json\/","title":{"rendered":"Asp.NET MVC &#8211; JSON"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Yap\u0131sal olarak XML diline benzeyen, veri ta\u015f\u0131mak i\u00e7in olu\u015fturulmu\u015f standart bir Object JavaScript dilidir.JSON(<em><strong>JavaScript Object Notation<\/strong><\/em>), JavaScript i\u00e7erisinde kolayca i\u015flenebilme \u00f6zelli\u011fine sahiptir.Bu \u00f6zelli\u011fi y\u00fcz\u00fcnden genellikle Ajax platformunda tercih edilmektedir.JSON ile d\u00fczenlenen bir veri yap\u0131s\u0131na JavaScript komutlar\u0131yla \u00e7ok h\u0131zl\u0131 ve esnek bir \u015fekilde eri\u015filebilmektedir.Yap\u0131sal olarak her ne kadar XML diline benzesede, XML&#8217;e g\u00f6re daha h\u0131zl\u0131 parse edilmekte ve fiziksel boyut olarakta az yer kaplamaktad\u0131r.Bunun nedeni ise, JSON&#8217;da XML&#8217;de oldu\u011fu gibi &#8220;node&#8221; a\u00e7\u0131p kapatmayla u\u011fra\u015f\u0131lmamakta, onun yerine sat\u0131rlar ve elementler birbirlerinden noktalama i\u015faretleri ve parantezler ile ayr\u0131lmaktad\u0131rlar.\u015eimdi gelin beraber Asp.NET MVC yap\u0131s\u0131yla JSON mimarisini birlikte daha detayl\u0131 inceleyelim.<br \/>\n<!--more--><\/p>\n<p>\u00d6ncelikle JSON yap\u0131s\u0131nda kullan\u0131lan veri tiplerini g\u00f6relim.<br \/>\n&#8220;<span style=\"color: #3366ff;\"><strong>string<\/strong><\/span>&#8220;, &#8220;<span style=\"color: #3366ff;\"><strong>number<\/strong><\/span>&#8220;, &#8220;<span style=\"color: #3366ff;\"><strong>bool<\/strong><\/span>&#8220;, &#8220;<strong><span style=\"color: #3366ff;\">array<\/span><\/strong>&#8220;, &#8220;<span style=\"color: #3366ff;\"><strong>object<\/strong><\/span>&#8221; ve &#8220;<strong><span style=\"color: #3366ff;\">null<\/span><\/strong>&#8221;<\/p>\n<p>\u015eimdi hemen bir Asp.NET MVC projesinde \u00f6rneklendirme yapal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public ActionResult Index()\r\n        {\r\n            return View();\r\n        }\r\n\r\n        public JsonResult OgrencileriGetir()\r\n        {\r\n            List&lt;ListItem&gt; Liste = new List&lt;ListItem&gt;() {\r\n            new ListItem{ Value = &quot;1&quot; , Text = &quot;Ahmet&quot;},\r\n            new ListItem{ Value = &quot;2&quot; , Text = &quot;Mehmet&quot;},\r\n            new ListItem{ Value = &quot;3&quot; , Text = &quot;Ali&quot;},\r\n            new ListItem{ Value = &quot;4&quot; , Text = &quot;Ay\u015fe&quot;},\r\n            new ListItem{ Value = &quot;5&quot; , Text = &quot;Gen\u00e7ay&quot;},\r\n            };\r\n\r\n            return Json(Liste, JsonRequestBehavior.AllowGet);\r\n        }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelersek e\u011fer, &#8220;JsonResult&#8221; tipinden &#8220;OgrencileriGetir&#8221; isimli metodumuzda bir \u00f6\u011frenci bilgisini tutan liste mevcut ve metodumuz bu listeyi JSON yap\u0131s\u0131nda d\u00f6nd\u00fcrmektedir.<\/p>\n<p>\u015eimdi bu listeyi &#8220;Index&#8221; isimli Action metodumuzun View katman\u0131ndan \u00e7ekece\u011fiz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\/\/ &lt;!&#x5B;CDATA&#x5B;\r\n    $(function () {\r\n        $('#btnOgrencileriGetir').click(function () {\r\n            $.getJSON(&quot;\/Home\/OgrencileriGetir&quot;, null, function (data) {\r\n                jQuery.each(data, function (i, eleman) {\r\n                    $(&quot;#OgrenciListesi&quot;).append(eleman.Value + &quot; &quot; + eleman.Text + &quot;&lt;br&gt;\r\n&quot;);\r\n                });\r\n            });\r\n        });\r\n    });\r\n&lt;\/script&gt;\r\n\r\n&lt;input id=&quot;btnOgrencileriGetir&quot; type=&quot;button&quot; value=&quot;\u00d6\u011frencileri Getir&quot; \/&gt;\r\n&lt;div id=&quot;OgrenciListesi&quot;&gt;&lt;\/div&gt;\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer, &#8220;btnOgrencileriGetir&#8221; isimli butona t\u0131kland\u0131\u011f\u0131 zaman tetiklenecek JavaScript kodunu yaz\u0131yoruz.&#8221;getJSON&#8221; metodu ile JSON verisini al\u0131yoruz.Birinci parametreye JSON d\u00f6nd\u00fcren metodumuzu, ikinci parametreye ise bu metoda g\u00f6nderece\u011fimiz parametreyi yaz\u0131yoruz.&#8221;<em><span style=\"color: #3366ff;\">function (data)<\/span><\/em>&#8221; metodu ile JSON verimiz &#8220;data&#8221; isimli parametreye y\u00fckleniyor.&#8221;<em><span style=\"color: #3366ff;\">jQuery.each<\/span><\/em>&#8221; metodu C#&#8217;ta ki &#8220;foreach&#8221; d\u00f6ng\u00fcs\u00fcne benzer niteliktedir ve bu metod sayesinde liste halinde gelen verimizi ayr\u0131\u015ft\u0131r\u0131p gerekli alana yans\u0131t\u0131yoruz.<\/p>\n<p>Bu i\u015flemin ekran g\u00f6r\u00fcnt\u00fcs\u00fc a\u015fa\u011f\u0131daki gibi olacakt\u0131r.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/AspNetMvcJSon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2349\" alt=\"AspNetMvcJSon\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/AspNetMvcJSon.png\" width=\"133\" height=\"129\" \/><\/a><\/p>\n<p>Buraya kadar Server&#8217;dan JSON format\u0131nda nas\u0131l veri al\u0131nd\u0131\u011f\u0131n\u0131 g\u00f6rd\u00fck.\u015eimdide Server&#8217;a JSON format\u0131nda veri g\u00f6ndermeyi inceleyelim.<\/p>\n<p>Bu \u00f6rneklendirmeyi bir model \u00fczerinden ger\u00e7ekle\u015ftirece\u011fimiz i\u00e7in \u00f6ncelike &#8220;Ogrenci&#8221; isminde bir model tan\u0131ml\u0131yorum.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Ogrenci\r\n    {\r\n        public int OgrenciNo { get; set; }\r\n        public string Adi { get; set; }\r\n        public string SoyAdi { get; set; }\r\n    }\r\n<\/pre>\n<p>\u015eimdi bu modeli POST edebilecek yap\u0131da bir View sayfas\u0131 tasarlayal\u0131m.(<span style=\"font-size: x-small;\">\u00d6rnek olarak &#8220;Home(Controller).cs&#8221; isimli Controller sayfas\u0131n\u0131n &#8220;Index&#8221; Action metodunun .cshtml uzant\u0131l\u0131 dosyas\u0131nda<\/span>)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div&gt;@Html.TextBox(&quot;OgrenciNo&quot;)&lt;\/div&gt;\r\n&lt;div&gt;@Html.TextBox(&quot;Adi&quot;)&lt;\/div&gt;\r\n&lt;div&gt;@Html.TextBox(&quot;SoyAdi&quot;)&lt;\/div&gt;\r\n&lt;div&gt;&lt;input id=&quot;btnGonder&quot; type=&quot;submit&quot; value=&quot;Ogrenciyi JSON Olarak G\u00f6nder&quot; \/&gt;&lt;\/div&gt;\r\n<\/pre>\n<p>\u015eimdi ise olu\u015fturdu\u011fumuz bu formdaki bilgileri Server&#8217;a JSON tabanl\u0131 POST edecek JQuery kodlar\u0131m\u0131z\u0131 yazal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n    $('#btnGonder').click(function () {\r\n        var Ogrenci = {\r\n            OgrenciNo: $('#OgrenciNo').val(),\r\n            Adi: $('#Adi').val(),\r\n            SoyAdi: $('#SoyAdi').val()\r\n        };\r\n        $.ajax({\r\n            url: '\/Home\/GelenOgrenci',\r\n            type: 'POST',\r\n            data: JSON.stringify(Ogrenci),\r\n            dataType: 'json',\r\n            contentType: 'application\/json; charset=utf-8'\r\n        });\r\n        return false;\r\n    });\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelersek e\u011fer, &#8220;btnGonder&#8221; isimli butonumuz tetiklendi\u011fi an &#8220;Home(Controller).cs&#8221; isimli Controller s\u0131n\u0131f\u0131ndaki &#8220;GelenOgrenci&#8221; isimli metodun \u00e7al\u0131\u015faca\u011f\u0131 bildirilmi\u015ftir.&#8221;data&#8221; parametresi ile JSON format\u0131nda olu\u015fturulan &#8220;Ogrenci&#8221; isimli nesnemiz Server&#8217;a g\u00f6nderilecektir.<\/p>\n<p>\u015eimdide son olarak &#8220;Home(Controller).cs&#8221; Controller s\u0131n\u0131f\u0131m\u0131zdaki &#8220;GelenOgrenci&#8221; metodunu olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class HomeController : Controller\r\n    {\r\n        public ActionResult Index()\r\n        {\r\n            return View();\r\n        }\r\n        public ActionResult GelenOgrenci(Ogrenci ogrenci)\r\n        {\r\n            return View();\r\n        }\r\n    }\r\n<\/pre>\n<p>Projemizi &#8220;GelenOgrenci&#8221; metoduna Break &#8211; Point koyarak, bu \u015fekilde derleyip \u00e7al\u0131\u015ft\u0131rd\u0131\u011f\u0131m\u0131z zaman a\u015fa\u011f\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcyle kar\u015f\u0131la\u015faca\u011f\u0131z.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/AspNetMvcJSon2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/AspNetMvcJSon2-300x56.png\" alt=\"AspNetMvcJSon2\" width=\"300\" height=\"56\" class=\"aligncenter size-medium wp-image-2353\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/AspNetMvcJSon2-300x56.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2013\/11\/AspNetMvcJSon2.png 499w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Son olarak dikkat etmemiz gereken bir hususa de\u011finmek gerekirse, JQuery k\u00fct\u00fcphanesindeki &#8220;JSON.stringify()&#8221; metodu IE9 ve \u00fczeri browserlarda desteklenmektedir.<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fcmidiyle..<br \/>\nFaydalanman\u0131z dile\u011fiyle..<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar..<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Yap\u0131sal olarak XML diline benzeyen, veri ta\u015f\u0131mak i\u00e7in olu\u015fturulmu\u015f standart bir Object JavaScript dilidir.JSON(JavaScript Object Notation), JavaScript i\u00e7erisinde kolayca i\u015flenebilme \u00f6zelli\u011fine sahiptir.Bu \u00f6zelli\u011fi y\u00fcz\u00fcnden genellikle Ajax platformunda tercih edilmektedir.JSON ile d\u00fczenlenen bir veri yap\u0131s\u0131na&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,780,345],"tags":[744,746,748,530,283],"class_list":["post-2344","post","type-post","status-publish","format-standard","hentry","category-asp-net-mvc","category-json","category-mvc-4-0","tag-ajax","tag-asp-net-mvc","tag-jquery","tag-json","tag-mvc"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/2344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=2344"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/2344\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=2344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=2344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=2344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}