JSON是用来干什么的,JObject转换的选择方法

By admin in 4858.com on 2019年4月8日

Linq to
JSON是用来操作JSON对象的.能够用于快捷查询,修改和创制JSON对象.当JSON对象内容相比复杂,而大家唯有须要中间的一小部分数据时,可以思考使用Linq
to JSON来读取和修改部分的多寡而非反体系化全部.

Linq to
JSON是用来操作JSON对象的.能够用来迅速查询,修改和开创JSON对象.当JSON对象内容比较复杂,而大家一味要求中间的一小部分数额时,能够设想动用Linq
to JSON来读取和修改部分的数目而非反系列化全体.

 Linq to
JSON是用来操作JSON对象的.能够用于快速查询,修改和成立JSON对象.当JSON对象内容比较复杂,而咱们唯有须要中间的一小部分数目时,能够思量选择Linq
to JSON来读取和修改部分的数额而非反连串化全体.

Json to JObject转换的选用方法,jsontojobject转换

Linq to
JSON是用来操作JSON对象的.能够用于火速查询,修改和创制JSON对象.当JSON对象内容比较复杂,而大家只有必要中间的一小部分数量时,可以思虑接纳Linq
to JSON来读取和改动部分的数目而非反系列化全部.

贰.创设JSON数组和对象

在展开Linq to JSON在此之前,首先要打听一下用于操作Linq to JSON的类.

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

 

 

 

 

 

 

 

JSON是用来干什么的,JObject转换的选择方法。 

1.创建JSON对象

4858.com 1

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

4858.com 2

结果:

4858.com 3

4858.com,除了那几个之外,仍是能够由此眨眼间间办法来博取JObject.JArray类似。

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

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

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

 

 

 

 

 

 

2.创建JSON数组

4858.com 4

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

4858.com 5

结果:

4858.com 6

三.使用Linq to JSON

1.查询
率先准备Json字符串,是贰个饱含职员和工人基本信息的Json

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

1获得该职员和工人的全名

4858.com 7

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

4858.com 8

结果:

4858.com 9

二获取该职员和工人同事的享有人名

4858.com 10

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

4858.com 11

“Children()”能够回去全部数组中的对象

结果:

4858.com 12

2.修改

壹现行反革命大家发现赢得的json字符串中杰克的岁数应当为35

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

结果:

4858.com 13

瞩目不要通过以下办法来修改:

            JObject jObj = JObject.Parse(json);
            JToken age = jObj["Age"];
            age = 35;

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

4858.com 14

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

4858.com 15

结果:

4858.com 16

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

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

结果:

4858.com 17

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

            JObject jObj = JObject.Parse(json);
            jObj["Colleagues"][1].Remove();
            Console.WriteLine(jObj.ToString());

结果:

4858.com 18

4.添加
壹大家发现Jack的音讯中少了部门音讯,供给大家必须添加在Age的前边

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

结果:

4858.com 19

2现行反革命我们又发现,杰克公司来了一个新同事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());

结果:

4858.com 20

肆.简化查询语句

运用函数SelectToken能够简化查询语句,具体:
壹使用SelectToken来询问名称

            JObject jObj = JObject.Parse(json);
            JToken name = jObj.SelectToken("Name");
            Console.WriteLine(name.ToString());

结果:

4858.com 21

2利用SelectToken来查询全部同事的名字

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

结果:

4858.com 22

叁查询最后一名同事的年纪

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

结果:

4858.com 23

 

二.开立JSON数组和指标

2.创立JSON数组和目的

2.创建JSON数组和目的

FAQ

1.万壹Json中的Key是转变的然则结构不变,如何取得所要的情节?

例如: 4858.com 24

 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 }

4858.com 25

内部的”20一3-0伍-31
1肆:3一”是生成的key,怎样收获在那之中的”name”,”query”,”amount”,”delta”等音讯呢?
因此Linq能够很简短地完毕:

4858.com 26

 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; }
}

to JObject转换的采纳办法,jsontojobject转换
Linq to
JSON是用来操作JSON对象的.能够用于火速查询,修改和创造JSON对象.当JSON对象内容相比复杂…

在进展Linq to JSON从前,首先要询问一下用来操作Linq to JSON的类.

在拓展Linq to JSON在此之前,首先要精通一下用以操作Linq to JSON的类.

在开始展览Linq to JSON在此之前,首先要领悟一下用以操作Linq to JSON的类.

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.创建JSON对象

1.创建JSON对象

1.创建JSON对象

4858.com 27

4858.com 28

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

 

4858.com 29

4858.com 30

结果:

结果:

结果:

4858.com 31

4858.com 32

4858.com 33

除开,还足以由此眨眼之间间格局来赢得JObject.JArray类似。

而外,仍是能够透过刹那间主意来收获JObject.JArray类似。

除去,还足以经过弹指间方式来获得JObject.JArray类似。

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

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

JObject.Load(JsonReader reader)
reader包含着JSON对象的内容,返回一个JObject对象
方法   说明
JObject.Parse(string json)
json含有JSON对象的字符串,返回为JObject对象
JObject.FromObject(object o)

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

JObject.Load(JsonReader reader)
reader包含着JSON对象的内容,返回一个JObject对象
方法   说明
JObject.Parse(string json)
json含有JSON对象的字符串,返回为JObject对象
JObject.FromObject(object o)

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

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.创建JSON数组

2.创建JSON数组

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

4858.com 34

4858.com 35

 

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

结果:

4858.com 36

4858.com 37

4858.com 38

结果:

结果:

三.使用Linq to JSON

4858.com 39

4858.com 40

1.查询
首先准备Json字符串,是多少个涵盖职员和工人基本音信的Json

三.使用Linq to JSON

三.使用Linq to JSON

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

1.查询
先是准备Json字符串,是1个含有员工基本音讯的Json

1.查询
第3准备Json字符串,是一个分包职员和工人基本音讯的Json

 

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

壹收获该员工的全名

1拿走该职员和工人的人名

壹获得该职员和工人的全名

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

4858.com 41

4858.com 42

 

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

结果:

4858.com 43

4858.com 44

4858.com 45

结果:

结果:

贰获取该员工同事的拥有人名

4858.com 46

4858.com 47

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

贰获取该职工同事的全数人名

二获取该职工同事的富有人名

 

4858.com 48

4858.com 49

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

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

结果:

4858.com 50

4858.com 51

4858.com 52

“Children()”能够回去全部数组中的对象

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

2.修改

结果:

结果:

壹现行反革命我们发现赢得的json字符串中杰克的年龄应有为3伍

4858.com 53

4858.com 54

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

2.修改

2.修改

 

一现行反革命大家发现赢得的json字符串中杰克的年龄应当为35

壹现行反革命我们发现赢得的json字符串中杰克的年华应有为3五

结果:

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

4858.com 55

结果:

结果:

瞩目不要通过以下措施来修改:

4858.com 56

4858.com 57

            JObject jObj = JObject.Parse(json);
            JToken age = jObj["Age"];
            age = 35;

注意不要通过以下办法来修改:

小心不要通过以下方法来修改:

 

            JObject jObj = JObject.Parse(json);
            JToken age = jObj["Age"];
            age = 35;
            JObject jObj = JObject.Parse(json);
            JToken age = jObj["Age"];
            age = 35;

二现行反革命大家发现杰克的同事汤姆的年龄错了,应该为四伍

2现行反革命咱们发现杰克的同事Tom的年华错了,应该为肆五

二现行反革命大家发现杰克的同事Tom的年华错了,应该为四伍

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

4858.com 58

4858.com 59

 

            //将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 colleagues = jObj["Colleagues"];
            colleagues[0]["Age"] = 45;
            jObj["Colleagues"] = colleagues;//修改后,再赋给对象
            Console.WriteLine(jObj.ToString());

结果:

4858.com 60

4858.com 61

4858.com 62

结果:

结果:

3.删除
壹现行大家想删除杰克的同事

4858.com 63

4858.com 64

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

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

3.删除
1现行反革命大家想删除杰克的同事

 

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

 

结果:

结果:

结果:

4858.com 65

4858.com 66

4858.com 67

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

2现行反革命我们发现Abel不是杰克的同事,供给从中删除

贰现行反革命大家发现阿贝l不是杰克的同事,供给从中删除

            JObject jObj = JObject.Parse(json);
            jObj["Colleagues"][1].Remove();
            Console.WriteLine(jObj.ToString());
            JObject jObj = JObject.Parse(json);
            jObj["Colleagues"][1].Remove();
            Console.WriteLine(jObj.ToString());
            JObject jObj = JObject.Parse(json);
            jObj["Colleagues"][1].Remove();
            Console.WriteLine(jObj.ToString());

结果:

结果:

 

4858.com 68

4858.com 69

结果:

4.添加
壹我们发现杰克的音信中少了单位音讯,须求大家不可能不添加在Age的末端

4.添加
壹我们发现杰克的新闻中少了单位新闻,须求大家亟须添加在Age的背后

4858.com 70

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

4.添加
一大家发现杰克的音讯中少了单位音讯,供给大家亟须添加在Age的背后

结果:

结果:

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

4858.com 71

4858.com 72

 

二现行反革命我们又发现,杰克公司来了贰个新同事琳达

二现行反革命我们又发现,杰克公司来了1个新同事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());
            //将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());

4858.com 73

结果:

结果:

贰现行反革命我们又发现,杰克企业来了三个新同事琳达

4858.com 74

4858.com 75

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

四.简化查询语句

肆.简化查询语句

 

动用函数SelectToken能够简化查询语句,具体:
壹使用SelectToken来询问名称

采纳函数SelectToken能够简化查询语句,具体:
一施用SelectToken来询问名称

结果:

            JObject jObj = JObject.Parse(json);
            JToken name = jObj.SelectToken("Name");
            Console.WriteLine(name.ToString());
            JObject jObj = JObject.Parse(json);
            JToken name = jObj.SelectToken("Name");
            Console.WriteLine(name.ToString());

4858.com 76

结果:

结果:

四.简化查询语句

4858.com 77

4858.com 78

应用函数SelectToken能够简化查询语句,具体:
1应用SelectToken来查询名称

2施用SelectToken来查询全数同事的名字

2行使SelectToken来询问全数同事的名字

            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());
            JObject jObj = JObject.Parse(json);
            var names = jObj.SelectToken("Colleagues").Select(p => p["Name"]).ToList();
            foreach (var name in names)
                Console.WriteLine(name.ToString());

 

结果:

结果:

 

4858.com 79

4858.com 80

结果:

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

3查询最后一名同事的岁数

4858.com 81

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

2运用SelectToken来询问全部同事的名字

结果:

结果:

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

4858.com 82

4858.com 83

 

 

 

结果:

FAQ

壹.比方Json中的Key是变化的只是结构不变,怎么着获得所要的剧情?

例如:

4858.com 84

 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 }

4858.com 85

里面包车型客车”20一三-05-31
1四:3壹”是转变的key,怎么样取得在那之中的”name”,”query”,”amount”,”delta”等音信呢?
经过Linq可以很简短地形成:

4858.com 86

 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; }
}

FAQ

壹.借使Json中的Key是转变的而是结构不变,如何获得所要的始末?

例如:

4858.com 87

 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 }

4858.com 88

里头的”201三-0伍-31
1四:3壹”是浮动的key,怎么样获得在这之中的”name”,”query”,”amount”,”delta”等音讯吗?
因此Linq能够非常粗略地达成:

4858.com 89

 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; }
}

4858.com 90

3询问最后一名同事的岁数

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

 

结果:

4858.com 91

 

FAQ

一.假诺Json中的Key是转变的可是结构不变,如何收获所要的内容?

例如:

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

 

内部的”201三-0伍-31
14:3壹”是浮动的key,如何收获当中的”name”,”query”,”amount”,”delta”等音讯吗?
由此Linq能够很简短地完毕:

 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; }
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有