Asp.net C# ile Json verisi okuma
11 Mart 2020 / 17:01
Burak
Merhabalar,
Öncelikle buradan ” Newtonsoft.Json.dll (4.5)” ( Json netframework 3.5 için olanı ise buradan indirebilirsiniz ) dosyamızı indirelim. Daha sonra kütüphanelerimizi yüklüyoruz.
1 2 3 4 5 6 7 8 9 10 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Newtonsoft.Json; using System.IO; using System.Net; using System.Text.RegularExpressions; |
Class’ımızı oluşturuyoruz. Burdaki objeleri jsondaki verilere göre kendinizi oluşturucaksınız
Örnek class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class RootObject { public int SEMBOLID { get; set; } public string SEMBOL { get; set; } public string ACIKLAMA { get; set; } public double YUZDEDEGISIM { get; set; } public double NET { get; set; } public DateTime TARIH { get; set; } public double YUKSEK { get; set; } public double DUSUK { get; set; } public double DUNKUKAPANIS { get; set; } public double ACILIS { get; set; } public double KAPANIS { get; set; } public double ALIS { get; set; } public double SATIS { get; set; } public double HACIMTL { get; set; } public int OrderId { get; set; } } |
Daha sonra page load kısmına aşağıdaki kodları yapıştırın gitsin 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 |
HttpWebRequest request = WebRequest.Create("json urlsi") as HttpWebRequest; string jsonVerisi = ""; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); jsonVerisi = reader.ReadToEnd(); } IEnumerable<RootObject> result = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(jsonVerisi ); foreach (RootObject r in result) { Response.Write(r.SEMBOL + " : " + r.SATIS + " Değişim "+r.YUZDEDEGISIM+"<br>"); } |
Bir başka örnek kod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Program { static void Main(string[] args) { string data = "{\"id\": \"100\", \"ad\": \"Sanal\", \"soyad\": \"Data\"}"; Kisi k = JsonConvert.DeserializeObject<Kisi>(data); Console.Write(k.ad); Console.ReadLine(); } } public class Kisi { public int id { get; set; } public string ad { get; set; } public string soyad { get; set; } } |
Eğerki json yapınızda alt bir json varsa yani aşağıdak gibiyse
Aşağıdaki gibi json okuma işlemlerini yapabilirsiniz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
protected void Page_Load(object sender, EventArgs e) { try { string kelime = Request.QueryString["kelime"]; ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; HttpWebRequest request = WebRequest.Create("json linki") as HttpWebRequest; string data = ""; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); data = reader.ReadToEnd(); } icerikler k = JsonConvert.DeserializeObject<icerikler>(data); for (Int32 i = 0; i <= 199; i++) { Response.Write(k.hits[i].id + " = " + k.hits[i].tags + " = " + k.hits[i].largeImageURL+"<br>"); } } catch(Exception exp) { Response.Write(exp.Message); } } public class icerikler { public int total { get; set; } public List<resimler> hits { get; set; } } public class resimler { public string largeImageURL { get; set; } public string tags { get; set; } public int id { get; set; } } |
Etiketler: asp.net json okuma, asp.net json read, asp.net sub json elements read, c# json okuma, json okuma
Bu Yazılarıda Okuyabilirsiniz...
Bir cevap yazın