Last active
September 24, 2015 13:23
-
-
Save ivankahl/11a880fe2acfddc3c710 to your computer and use it in GitHub Desktop.
Serialize and Deserialize JSON data in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Hamley's", | |
"toys": [{ | |
"name": "Teddy", | |
"price": 100, | |
"suitableAge": 7 | |
}, | |
{ | |
"name": "Lego", | |
"price": 700, | |
"suitableAge": 12 | |
}, | |
{ | |
"name": "Barbie", | |
"price": 150, | |
"suitableAge": 10 | |
}, | |
{ | |
"name": "Polly Pocket", | |
"price": 200, | |
"suitableAge": 11 | |
}] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Runtime.Serialization.Json; | |
using System.IO; | |
namespace JSONSerializerDesarializer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string toyData = File.ReadAllText("C:\\Users\\Ivan\\data.json"); | |
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(ToyStore)); | |
MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(toyData)); | |
ToyStore toyStore = (ToyStore)js.ReadObject(ms); | |
Console.WriteLine("Name: {0}", toyStore.Name); | |
foreach (Toy toy in toyStore.Toys) | |
{ | |
Console.WriteLine(toy.Name); | |
Console.WriteLine(new String('=', toy.Name.Length)); | |
Console.WriteLine("Price: {0}", toy.Price); | |
Console.WriteLine("Suitable Age: {0}", toy.SuitableAge); | |
Console.WriteLine(); | |
} | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Runtime.Serialization.Json; | |
using System.IO; | |
namespace JSONSerializerDesarializer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ToyStore toyStore = new ToyStore("Hamley's"); | |
toyStore.AddToy(new Toy("Teddy", 100, 7)); | |
toyStore.AddToy(new Toy("Lego", 700, 12)); | |
toyStore.AddToy(new Toy("Barbie", 150, 10)); | |
toyStore.AddToy(new Toy("Polly Pocket", 200, 11)); | |
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(ToyStore)); | |
MemoryStream ms = new MemoryStream(); | |
js.WriteObject(ms, toyStore); | |
ms.Position = 0; | |
FileStream fs = File.Create("C:\\Users\\Ivan\\data.json"); | |
ms.CopyTo(fs); | |
fs.Close(); | |
ms.Close(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.Serialization; | |
namespace JSONSerializerDesarializer | |
{ | |
[DataContract(Name = "toy")] | |
public class Toy | |
{ | |
private string _name; | |
private int _price; | |
private int _suitableAge; | |
public Toy(string name, int price, int suitableAge) | |
{ | |
_name = name; | |
_price = price; | |
_suitableAge = suitableAge; | |
} | |
[DataMember(Name = "name")] | |
public string Name | |
{ | |
get | |
{ | |
return _name; | |
} | |
set | |
{ | |
_name = value; | |
} | |
} | |
[DataMember(Name = "price")] | |
public int Price | |
{ | |
get | |
{ | |
return _price; | |
} | |
set | |
{ | |
_price = value; | |
} | |
} | |
[DataMember(Name = "suitableAge")] | |
public int SuitableAge | |
{ | |
get | |
{ | |
return _suitableAge; | |
} | |
set | |
{ | |
_suitableAge = value; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Runtime.Serialization; | |
namespace JSONSerializerDesarializer | |
{ | |
[DataContract(Name = "toyStore")] | |
public class ToyStore | |
{ | |
private string _name; | |
private List<Toy> _toys; | |
public ToyStore(string name) | |
{ | |
_name = name; | |
_toys = new List<Toy> { }; | |
} | |
[DataMember(Name = "name")] | |
public string Name | |
{ | |
get | |
{ | |
return _name; | |
} | |
set | |
{ | |
_name = value; | |
} | |
} | |
[DataMember(Name = "toys")] | |
public List<Toy> Toys | |
{ | |
get | |
{ | |
return _toys; | |
} | |
private set | |
{ | |
_toys = value; | |
} | |
} | |
public void AddToy(Toy toy) | |
{ | |
Toys.Add(toy); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment