一、什么是 LINQ TO JSON
Linq to JSON
是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象。
二.创建JSON数组和对象
用于操作Linq to JSON
的类.
类名 | 说明 |
---|---|
JObject | 用于操作JSON对象 |
JArray | 用语操作JSON数组 |
JValue | 表示数组中的值 |
JProperty | 表示对象中的属性,以"key/value"形式 |
JToken | 用于存放Linq to JSON查询后的结果 |
1.创建JSON对象
JObject jobj= new JObject(); jobj.Add(new JProperty("Name", "Jack")); jobj.Add(new JProperty("Age", 33)); jobj.Add(new JProperty("Department", "Personnel Department")); jobj.Add(new JProperty("Leader", new JObject(new JProperty("Name", "Tom"), new JProperty("Age", 44), new JProperty("Department", "Personnel Department")))); Console.WriteLine(staff.ToString());
输出结果:
除此之外,还可以通过一下方式来获取
方法 | 说明 |
---|---|
JObject.Parse(string json) |
json含有JSON对象的字符串,返回为JObject对象 |
JObject.FromObject(object o) |
o为要转化的对象,返回一个JObject对象 |
JObject.Load(JsonReader reader) |
reader包含着JSON对象的内容,返回一个JObject对象 |
2.创建JSON数组
JArray arr = new JArray(); arr.Add(new JValue(1)); arr.Add(new JValue(2)); arr.Add(new JValue(3)); Console.WriteLine(arr.ToString());
输出结果:
方法 | 说明 |
---|---|
JArray.Parse(string json) |
json含有JSON数组的字符串,返回为JArray数组 |
JArray.FromObject(object o) |
o为要转化的对象,返回一个JArray数组 |
JArray.Load(JsonReader reader) |
reader包含着JSON对象的内容,返回一个JArray数组 |
三.使用Linq to JSON
1.查询
首先准备json字符串,是一个包含员工基本信息的json
string json = "{"Name" : "Jack", "Age" : 34, "Colleagues" : [{"Name" : "Tom" , "Age":44},{"Name" : "Abel","Age":29}] }";
①获取该员工的姓名
//将json转换为JObject JObject jObj = JObject.Parse(json); //通过属性名或者索引来访问,仅仅是自己的属性名,而不是所有的 JToken nameToken = jObj["Name"]; Console.WriteLine(nameToken.ToString());
输出:Jack
②获取该员工同事的所有姓名
//将json转换为JObject JObject jObj = JObject.Parse(json); var names=from staff in jObj["Colleagues"].Children() select (string)staff["Name"]; foreach (var name in names) Console.WriteLine(name);
"Children()"可以返回所有数组中的对象
输出:
Tom
Abel
2.修改
①现在我们发现获取的json字符串中Jack的年龄应该为35
//将json转换为JObject JObject jObj = JObject.Parse(json); jObj["Age"] = 35; Console.WriteLine(jObj.ToString());
输出:(Jack年龄已经更新)
{ "Name": "Jack", "Age": 35, "Colleagues": [ { "Name": "Tom", "Age": 44 }, { "Name": "Abel", "Age": 29 } ] }
注意不要通过以下方式来修改:
JObject jObj = JObject.Parse(json); JToken age = jObj["Age"]; age = 35;
②现在我们发现Jack的同事Tom的年龄错了,应该为45
//将json转换为JObject JObject jObj = JObject.Parse(json); JToken colleagues = jObj["Colleagues"]; colleagues[0]["Age"] = 45; jObj["Colleagues"] = colleagues;//修改后,再赋给对象 Console.WriteLine(jObj.ToString());
输出:(tom年龄已经更新)
{ "Name": "Jack", "Age": 34, "Colleagues": [ { "Name": "Tom", "Age": 45 }, { "Name": "Abel", "Age": 29 } ] }
3.删除
①现在我们想删除Jack的同事
JObject jObj = JObject.Parse(json); jObj.Remove("Colleagues");//跟的是属性名称 Console.WriteLine(jObj.ToString());
输出:(已删除Jack的同事)
{ "Name": "Jack", "Age": 34 }
②现在我们发现Abel不是Jack的同事,要求从中删除
JObject jObj = JObject.Parse(json); jObj["Colleagues"][1].Remove(); Console.WriteLine(jObj.ToString());
输出:(已删除Abel)
{ "Name": "Jack", "Age": 34, "Colleagues": [ { "Name": "Tom", "Age": 44 } ] }
4.添加
①我们发现Jack的信息中少了部门信息,要求我们必须添加在Age的后面
//将json转换为JObject JObject jObj = JObject.Parse(json); jObj["Age"].Parent.AddAfterSelf(new JProperty("Department", "Personnel Department")); Console.WriteLine(jObj.ToString());
输出:
{ "Name": "Jack", "Age": 34, "Department": "Personnel Department", "Colleagues": [ { "Name": "Tom", "Age": 44 }, { "Name": "Abel", "Age": 29 } ] }
②现在我们又发现,Jack公司来了一个新同事Linda
//将json转换为JObject JObject jObj = JObject.Parse(json); JObject linda = new JObject(new JProperty("Name", "Linda"), new JProperty("Age", "23")); jObj["Colleagues"].Last.AddAfterSelf(linda); Console.WriteLine(jObj.ToString());
输出:
{ "Name": "Jack", "Age": 34, "Colleagues": [ { "Name": "Tom", "Age": 44 }, { "Name": "Abel", "Age": 29 }, { "Name": "Linda", "Age": "23" } ] }