1 public class Person
 2 {
 3 public string Name { get; set; }
 4 public int Age { get; set; }
 5 public DateTime Birthday { get; set; }
 6 }
 7 public class JsonSample
 8 {
 9 public static void Main()
10 {
11 PersonToJson();
12 JsonToPerson();
13 }
14 public static void PersonToJson()
15 {
16 Person bill = new Person();
17 bill.Name = "William Shakespeare";
18 bill.Age = 51;
19 bill.Birthday = new DateTime(1564, 4, 26);
20 string json_bill = JsonMapper.ToJson(bill);
21 Console.WriteLine(json_bill);
22 }
23 public static void JsonToPerson()
24 {
25 string json = @"
26 {
27 ""Name""     : ""Thomas More"",
28 ""Age""     : 57,
29 ""Birthday"" : ""02/07/1478 00:00:00""
30 }";
31 Person thomas = JsonMapper.ToObject<Person>(json);
32 Console.WriteLine("Thomas' age: {0}", thomas.Age);
33 }
34 }

输出:

posted on 2016-06-30 16:11  江春暮雪  阅读(142)  评论(0编辑  收藏  举报