1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 ///导入命名空间LitJson 8 using LitJson; 9 namespace ConsoleApplication1 10 { 11 /// <summary> 12 /// 定义一个人物类 13 /// </summary> 14 public class Person 15 { 16 public string name;//名称 17 public int age;//年龄 18 public DateTime birthday;//生日 19 } 20 public class Program 21 { 22 static void Main(string[] args) 23 { 24 PersonToJson(); 25 JsonToPerson(); 26 } 27 28 /// <summary> 29 ///文本转成Json格式 30 /// </summary> 31 public static void PersonToJson() 32 { 33 Person bill = new Person(); 34 bill.name = "LiuKe"; 35 bill.birthday = new DateTime(1997, 7, 30); 36 37 string json_bill = JsonMapper.ToJson(bill); 38 39 Console.WriteLine(json_bill); 40 } 41 /// <summary> 42 /// Json转成文本格式 43 /// </summary> 44 public static void JsonToPerson() 45 { 46 string json = @"{ 47 ""name"":""huangyi"", 48 ""age"":20, 49 ""birthday"":""02/07/1478 00:00:00"" 50 }"; 51 52 Person huangyi = JsonMapper.ToObject<Person>(json); 53 Console.WriteLine("name:{0},age:{1},birthday:{2}", huangyi.name, huangyi.age, huangyi.birthday); 54 } 55 } 56 }