![]()
Json hızlı ve sorunsuz veri akışından dolayı sıklıkla kullanılan bir formattır. C# ile JSON işlemlerini gerçekleştirmek için System.Text.Json kütüphanesi yada Newtonsoft kütüphanesi kullanılır. JSON verilerini serialize (nesneden JSON’a dönüştürme) ve deserialize (JSON’dan nesneye dönüştürme) işlemlerini nasıl gerçekleştirebileceğinizi bakacağız. Sırası ile 3 örnek ile bakalım,
1-En basit hali ile localden bakalım;
|
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 |
Console.WriteLine(“—————–Json Serialize———————-“); Person person = new Person { Name = “Yasin”, Age = 25, Salary = 57.500m, IsActive = true, Date = DateTime.Now }; string jsonString = JsonSerializer.Serialize(person); Console.WriteLine(jsonString); Console.WriteLine(“—————–Json Deserialize———————-“); string json = “{\”Name\”:\”Abuzer\”,\”Age\”:30,\”Salary\”:57.500,\”IsActive\”:true,\”Date\”:\”2025-06-20T09:34:46.6505838+03:00\”}”; Person person2 = JsonSerializer.Deserialize<Person>(json); Console.WriteLine( $“Name:{person2.Name},\n” + $“Age:{person2.Age},\n” + $“Salary:{person2.Salary},\n” + $“IsActive:{person2.IsActive},\n” + $“Date:{person2.Date},\n”); public class Person { public string Name { get; set; } public int Age { get; set; } public decimal Salary { get; set; } public bool IsActive { get; set; } public DateTime Date { get; set; } } |
Çıktısı:

2.Olarak localden bir Json dosyası çekerek yapalım:
|
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 44 45 46 47 48 49 50 |
//Path to the JSON file string filePath = @“C:……\Downloads\sample2.json”; //Read the entire content of the JSON file into a string var jsonP = File.ReadAllText(filePath); Root root = JsonSerializer.Deserialize<Root>(jsonP); Console.WriteLine( $“Bow:{root.bow},\n” + $“Darkness:{root.darkness},\n” + $“Proper:{root.proper},\n” + $“Worse:{root.worse},\n” + $“Tree:{root.tree},\n” + “————-See————–\n” + $“star: {root.see.star},\n” + $“chief: {root.see.chief},\n” + $“sugar:{root.see.sugar},\n” + $“straight:{root.see.straight},\n” + $“cake:{root.see.cake},\n” + “————-These————–\n” + $“Number Count:{root.see.these.Count},\n”); foreach (var item in root.see.these) { Console.WriteLine(“ValueKind : “ + item); } //class public class Root { public bool bow { get; set; } public int darkness { get; set; } public string proper { get; set; } public int worse { get; set; } public See see { get; set; } public int tree { get; set; } } public class See { public List<object> these { get; set; } public string star { get; set; } public string chief { get; set; } public bool sugar { get; set; } public string straight { get; set; } public bool cake { get; set; } } |
Çıktısı:

3. Olarak ta net’ten bir Json uzantısı çekip işlemleri yapalım:
|
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 |
string url = @“https://jsonplaceholder.typicode.com/posts/1”; string jsonVerisi = “”; using (WebClient response = new WebClient()) { jsonVerisi = response.DownloadString(url); } RootOnline jsonOnline = JsonConvert.DeserializeObject<RootOnline>(jsonVerisi); Console.WriteLine( $“userId:{jsonOnline.userId},\n” + $“Id:{jsonOnline.id},\n” + $“Title:{jsonOnline.title},\n” + $“Body:{jsonOnline.body},\n”); //class public class RootOnline { public int userId { get; set; } public int id { get; set; } public string title { get; set; } public string body { get; set; } } |
Çıktısı:

Olabilecek ihtimallerde Json formatında bir data nasıl Serialize yada Deserialize çevrilip kullanılır onu yapmış olduk. Kodlara adresinden ulaşabilirsiniz.
sağlıcakla kalın…








Bir yanıt yazın