using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleJsonSerializeOject
{
class Program
{
static void Main(string[] args)
{
Serialize0();
}
static void Serialize0()
{
try
{
Musteri ms1 = new Musteri()
{
AdSoyad = "ali",
MusteriID = 1,
MusteriKodu = "m19001",
Telefonlar = new List<string>()
{
"111 121 11 22",
"111 123 33 44"
},
Adresler = new Musteri.Adres()
{
Ulke = "Ulke 1",
Sehir = "Sehir 1",
Semt = "Semt 1",
Sokak = "Sokak 1"
}
};
Musteri ms2 = new Musteri()
{
AdSoyad = "ayşe",
MusteriID = 2,
MusteriKodu = "m19002",
Telefonlar = new List<string>()
{
"333 121 11 22",
"333 123 33 44"
},
Adresler = new Musteri.Adres()
{
Ulke = "Ulke 2",
Sehir = "Sehir 2",
Semt = "Semt 2",
Sokak = "Sokak 2"
}
};
List<Musteri> m0 = new List<Musteri>();
m0.Add(ms1);
m0.Add(ms2);
string strJson = JsonConvert.SerializeObject(m0);
File.WriteAllText(@"C:\testC\Json\jsonMusteri.json", strJson);
Console.WriteLine(" Serialized ");
//...........................................
string s1 = File.ReadAllText(@"C:\testC\Json\jsonMusteri.json");
var t0 = JsonConvert.DeserializeObject<List<Musteri>>(s1);
foreach (Musteri item in t0)
{}
//...........................................
Console.WriteLine("de Serialized ");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
}
//...........................................
public class Musteri
{
public int MusteriID { get; set; }
public string MusteriKodu { get; set; }
public string AdSoyad { get; set; }
public List<string> Telefonlar { get; set; }
public Adres Adresler { get; set; }
public class Adres
{
public string Ulke { get; set; }
public string Sehir { get; set; }
public string Semt { get; set; }
public string Sokak { get; set; }
}
}
}
}
Yorumlar
Yorum Gönder