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; } } |
Etiketler: asp.net json read
Bu Yazılarıda Okuyabilirsiniz...
Bir cevap yazın