2.修改

结果:

 

json 1

json 2

 

 

json 3

json 4

json 5

json 6

 

string json = "{\"Name\" : \"Jack\", \"Age\" : 34, \"Colleagues\" : [{\"Name\" : \"Tom\" , \"Age\":44},{\"Name\" : \"Abel\",\"Age\":29}] }";

json 7

 

结果:

json 8

json 9

结果:

结果:

 

json 10

类名 说明
JObject
 用于操作JSON对象
JArray
 用语操作JSON数组
JValue
 表示数组中的值
JProperty
 表示对象中的属性,以"key/value"形式
JToken
 用于存放Linq to JSON查询后的结果

结果:

 

二现行反革命我们又发现,杰克集团来了二个新同事Linda

json 11

结果:

            //将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);

1.创建JSON对象

json 12

结果:

结果:

json 13

三询问最终一名同事的年龄

json 14

二利用SelectToken来询问全部同事的名字

结果:

            //将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());

贰.开立JSON数组和对象

            JObject jObj = JObject.Parse(json);
            JToken name = jObj.SelectToken("Name");
            Console.WriteLine(name.ToString());
            JObject jObj = JObject.Parse(json);
            var names = jObj.SelectToken("Colleagues").Select(p => p["Name"]).ToList();
            foreach (var name in names)
                Console.WriteLine(name.ToString());

json 15

一拿走该职工的姓名

            //将json转换为JObject
            JObject jObj = JObject.Parse(json);
            jObj["Age"].Parent.AddAfterSelf(new JProperty("Department", "Personnel Department"));
            Console.WriteLine(jObj.ToString());

3.删除
一现行反革命大家想删除Jack的同事

4.添加
壹我们发现杰克的音讯中少了单位音信,须要大家亟须添加在Age的末尾

二现行反革命大家发现Abel不是杰克的同事,要求从中删除

 

 

            //将json转换为JObject
            JObject jObj = JObject.Parse(json);
            JToken colleagues = jObj["Colleagues"];
            colleagues[0]["Age"] = 45;
            jObj["Colleagues"] = colleagues;//修改后,再赋给对象
            Console.WriteLine(jObj.ToString());

 

            //将json转换为JObject
            JObject jObj = JObject.Parse(json);
            //通过属性名或者索引来访问,仅仅是自己的属性名,而不是所有的
            JToken ageToken =  jObj["Age"];
            Console.WriteLine(ageToken.ToString());

 

FAQ

壹.1旦Json中的Key是浮动的但是结构不变,如何收获所要的内容?

例如:

json 16

 1 {
 2 "trends":
 3 {
 4 "2013-05-31 14:31":
 5 [
 6 {"name":"我不是谁的偶像",
 7 "query":"我不是谁的偶像",
 8 "amount":"65172",
 9 "delta":"1596"},
10 {"name":"世界无烟日","query":"世界无烟日","amount":"33548","delta":"1105"},
11 {"name":"最萌身高差","query":"最萌身高差","amount":"32089","delta":"1069"},
12 {"name":"中国合伙人","query":"中国合伙人","amount":"25634","delta":"2"},
13 {"name":"exo回归","query":"exo回归","amount":"23275","delta":"321"},
14 {"name":"新一吻定情","query":"新一吻定情","amount":"21506","delta":"283"},
15 {"name":"进击的巨人","query":"进击的巨人","amount":"20358","delta":"46"},
16 {"name":"谁的青春没缺失","query":"谁的青春没缺失","amount":"17441","delta":"581"},
17 {"name":"我爱幸运七","query":"我爱幸运七","amount":"15051","delta":"255"},
18 {"name":"母爱10平方","query":"母爱10平方","amount":"14027","delta":"453"}
19 ]
20 },
21 "as_of":1369981898
22 }

json 17

其间的”20一3-0五-31
1四:31″是转变的key,如何收获当中的”name”,”query”,”amount”,”delta”等新闻呢?
透过Linq能够很简短地做到:

json 18

 var jObj = JObject.Parse(jsonString);
            var tends = from c in jObj.First.First.First.First.Children()
                        select JsonConvert.DeserializeObject<Trend>(c.ToString());
public class Trend
{
            public string Name { get; set; }
            public string Query { get; set; }
            public string Amount { get; set; }
            public string Delta { get; set; }
}

json 19

三.使用Linq to JSON

json 20

方法   说明
JObject.Parse(string json)
json含有JSON对象的字符串,返回为JObject对象
JObject.FromObject(object o)

o为要转化的对象,返回一个JObject对象

JObject.Load(JsonReader reader)
reader包含着JSON对象的内容,返回一个JObject对象

行使函数SelectToken可以简化查询语句,具体:
一施用SelectToken来查询名称

            JArray arr = new JArray();
            arr.Add(new JValue(1));
            arr.Add(new JValue(2));
            arr.Add(new JValue(3));
            Console.WriteLine(arr.ToString());

json 21

结果:

json 22

 

四.简化查询语句

json 23

json 24

2获取该职工同事的有所人名

“Children()”能够回到全体数组中的对象

结果:

1.查询
率先准备Json字符串,是贰个分包职员和工人基本新闻的Json

json 25

            //将json转换为JObject
            JObject jObj = JObject.Parse(json);
            var age = jObj.SelectToken("Colleagues[1].Age");
            Console.WriteLine(age.ToString());
            JObject jObj = JObject.Parse(json);
            jObj["Colleagues"][1].Remove();
            Console.WriteLine(jObj.ToString());
            JObject jObj = JObject.Parse(json);
            JToken age = jObj["Age"];
            age = 35;

而外,还足以经过须臾间方式来取得JObject.JArray类似。

2现行反革命我们发现Jack的同事汤姆的年纪错了,应该为四5

结果:

在实行Linq to JSON以前,首先要询问一下用以操作Linq to JSON的类.

壹现行大家发现赢得的json字符串中杰克的岁数应当为3伍

json 26

2.创建JSON数组

            //将json转换为JObject
            JObject jObj = JObject.Parse(json);
            jObj["Age"] = 35;
            Console.WriteLine(jObj.ToString());

 

结果:

 

            JObject staff = new JObject();
            staff.Add(new JProperty("Name", "Jack"));
            staff.Add(new JProperty("Age", 33));
            staff.Add(new JProperty("Department", "Personnel Department"));
            staff.Add(new JProperty("Leader", new JObject(new JProperty("Name", "Tom"), new JProperty("Age", 44), new JProperty("Department", "Personnel Department"))));
            Console.WriteLine(staff.ToString());

留神不要通过以下办法来修改:

            JObject jObj = JObject.Parse(json);
            jObj.Remove("Colleagues");//跟的是属性名称
            Console.WriteLine(jObj.ToString());

Linq to
JSON是用来操作JSON对象的.能够用于火速查询,修改和制造JSON对象.当JSON对象内容比较复杂,而大家唯有需求中间的一小部分数据时,能够考虑采取Linq
to JSON来读取和修改部分的数额而非反类别化全体.

相关文章

网站地图xml地图