JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。它基于 ECMAScript (w3c制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
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 }