JSON 入门

  JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互。

具体形式:

1. 对象是一个无序的“‘名称/值’对”集合

 {属性:值,属性:值,属性:值}

   eg: {name:"Peter Zhang",age:21,location:"Shanghai"}

   值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。

2. 数组是值(value)的有序集合

  [

    {name:"Peter",age:21,location:"Shanghai"},

    {name:"John Zhao",age:25,location:"Beijing"},

    {name:"Tina",age:23,location:"Heibei"},

  ]

C#中JSON的使用

 示例:

  1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Runtime.Serialization.Json;
5 using System.Runtime.Serialization;
6 using System.Text;
7
8 namespace JSON_Testing
9 {
10 [DataContract]
11 public class Student
12 {
13 [DataMember(Order = 0)]
14 public string Name
15 {
16 get;
17 set;
18 }
19
20 [DataMember(Order = 1)]
21 public int Age
22 {
23 get;
24 set;
25 }
26
27 [DataMember(Order = 2)]
28 public string Class
29 {
30 get;
31 set;
32 }
33
34 public override string ToString()
35 {
36 return string.Format("Student-> Name:{0}; Age:{1}; Class:{2}", Name, Age, Class);
37 }
38 }
39
40 public class JsonObjectConvert
41 {
42 /// <summary>
43 /// Convert Student object to Json string
44 /// </summary>
45 /// <param name="stu">student object</param>
46 /// <returns>json string</returns>
47 public static string ConvertObjectToJson(Student stu)
48 {
49 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Student));
50 var stream = new MemoryStream();
51 serializer.WriteObject(stream, stu);
52
53 byte[] bytes = new byte[stream.Length];
54
55 stream.Position = 0;
56
57 stream.Read(bytes, 0, (int)stream.Length);
58 stream.Close();
59
60 return Encoding.UTF8.GetString(bytes);
61 }
62
63 /// <summary>
64 /// Convert a list of students to Json string
65 /// </summary>
66 /// <param name="students">a student collection</param>
67 /// <returns>json string</returns>
68 public static string ConvertObjectsToJson(List<Student> students)
69 {
70 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Student>));
71 var stream = new MemoryStream();
72 serializer.WriteObject(stream, students);
73
74 byte[] bytes = new byte[stream.Length];
75
76 stream.Position = 0;
77
78 stream.Read(bytes, 0, (int)stream.Length);
79 stream.Close();
80
81 return Encoding.UTF8.GetString(bytes);
82 }
83
84 /// <summary>
85 /// Convert json string to student object
86 /// </summary>
87 /// <param name="jsonStr">json string</param>
88 /// <returns>student object</returns>
89 public static Student ConvertJsonToObject(string jsonStr)
90 {
91 byte[] bytes = Encoding.UTF8.GetBytes(jsonStr);
92 var stream = new MemoryStream(bytes);
93
94 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Student));
95 Student stu = (Student)serializer.ReadObject(stream);
96
97 stream.Close();
98 return stu;
99 }
100
101 /// <summary>
102 /// Convert json string to student list
103 /// </summary>
104 /// <param name="jsonStr">json string</param>
105 /// <returns>student list</returns>
106 public static List<Student> ConvertJsonToObjects(string jsonStr)
107 {
108 byte[] bytes = Encoding.UTF8.GetBytes(jsonStr);
109 var stream = new MemoryStream(bytes);
110
111 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Student>));
112 List<Student> students = (List<Student>)serializer.ReadObject(stream);
113
114 stream.Close();
115 return students;
116 }
117 }
118
119 class Program
120 {
121 static void Main(string[] args)
122 {
123 Student stu = new Student();
124 stu.Name = "John";
125 stu.Age = 20;
126 stu.Class = "01";
127
128 Student stu1 = new Student();
129 stu1.Name = "Peter";
130 stu1.Age = 23;
131 stu1.Class = "13";
132
133 List<Student> list = new List<Student>();
134 list.Add(stu);
135 list.Add(stu1);
136
137 string stuJson = JsonObjectConvert.ConvertObjectToJson(stu);
138 string stusJson = JsonObjectConvert.ConvertObjectsToJson(list);
139
140 Console.WriteLine("---------Convert student object to json string---------");
141 Console.WriteLine(stuJson);
142 Console.WriteLine("---------Convert list of student to json string---------");
143 Console.WriteLine(stusJson);
144
145 Student student = JsonObjectConvert.ConvertJsonToObject(stuJson);
146 List<Student> students = JsonObjectConvert.ConvertJsonToObjects(stusJson);
147
148 Console.WriteLine("---------Convert json string to student object---------");
149 Console.WriteLine(student);
150
151 Console.WriteLine("---------Convert json string to list of student---------");
152 foreach (Student s in students)
153 {
154 Console.WriteLine(s);
155 }
156 }
157 }
158 }

输出:

---------Convert student object to json string---------
{"Name":"John","Age":20,"Class":"01"}
---------Convert list of student to json string---------
[{"Name":"John","Age":20,"Class":"01"},{"Name":"Peter","Age":23,"Class":"13"}]
---------Convert json string to student object---------
Student-> Name:John; Age:20; Class:01
---------Convert json string to list of student---------
Student-> Name:John; Age:20; Class:01
Student-> Name:Peter; Age:23; Class:13

你可能感兴趣的:(json)