博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Newtonsoft.Json之JArray, JObject, JProperty,JValue
阅读量:6892 次
发布时间:2019-06-27

本文共 4372 字,大约阅读时间需要 14 分钟。

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

 

  

            JArray arr = new JArray();

            arr.Add(new JValue(1));

            arr.Add(new JValue(2));

            arr.Add(new JValue(3));

            Console.WriteLine(arr.ToString());

 

 

 

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

获取该员工的姓名

            //将json转换为JObject

            JObject jObj = JObject.Parse(json);

            //通过属性名或者索引来访问,仅仅是自己的属性名,而不是所有的

            JToken ageToken =  jObj["Age"];

            Console.WriteLine(ageToken.ToString());

 

 

 

获取该员工同事的所有姓名

 

            //将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()"可以返回所有数组中的对象

 

 

 

现在我们发现获取的json字符串中Jack的年龄应该为35

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

 

 

 

现在我们发现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());

 

 

 

删除

①现在我们想删除Jack的同事

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

 

 

 

现在我们发现Abel不是Jack的同事,要求从中删除

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

 

 

 

我们发现Jack的信息中少了部门信息,要求我们必须添加在Age的后面

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

 

 

 

现在我们又发现,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());

 

 

 

使用函数SelectToken可以简化查询语句,具体:

①利用SelectToken来查询名称

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

 

 

②利用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());

 

 

 

查询最后一名同事的年龄

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

 

 

 

 

 定义一个错误提示:

JObject errors = new JObject();

if (productName.Length <= 0)

        {

            errors.Add("ProductName", new JValue("该输入项为必输项"));

        }

 

//获取json里的值string jsonStr = "";//Json Str字符串            JToken json = JToken.Parse(jsonStr);//转化为JToken(JObject基类)            string xx = json.Value
("xx");//获取Json里xx键的值 JToken arr = json["arr"];//获取Json里的数组 {arr:[{yy:1,zz:2},{yy:3,zz:4}]} foreach (JToken baseJ in arr)//遍历数组 { int yy = baseJ.Value
("yy"); } string yy1 = json["arr"][0].Value
("yy");//也可以酱紫,多层的获取 string yy2 = json["arr"][0]["yy"] != null ? json["arr"][0]["yy"].ToString() : "";//这个和上面句等价,不要直接ToString,容易报错

 

JToken.ToObject Method

Overload List                        Name Description

Public method ToObject<T>()            Creates an instance of the specified .NET type from the JToken.
Public method ToObject(Type)           Creates an instance of the specified .NET type from the JToken.
Public method ToObject<T>(JsonSerializer)   Creates an instance of the specified .NET type from the JToken using the specified JsonSerializer.
Public method ToObject(Type, JsonSerializer)  Creates an instance of the specified .NET type from the JToken using the specified JsonSerializer.

 

http://www.cnblogs.com/usharei/archive/2012/04/24/2467578.html

你可能感兴趣的文章
Thread类常用方法
查看>>
路由重分布新技术实现多种路由协议不同网络间通信案例实操应用
查看>>
3月31日云栖精选夜读:数据科学咨询:想要转型毫无头绪?看了本文你不慌
查看>>
程序猿日记S01E03
查看>>
如何解决域名解析不生效问题?
查看>>
字符串指针修改问题
查看>>
通过JCONSOLE监控TOMCAT的JVM使用情况
查看>>
jquery editable plugin--点击编辑文字插件
查看>>
[Java] TreeMap、HashMap、LindedHashMap的区别
查看>>
MariaDB · 新特性 · 窗口函数
查看>>
thinkphp 3.2分布式数据库读写分离扩展阅读
查看>>
iOS流布局UICollectionView系列二——UICollectionView的代理方法
查看>>
我的友情链接
查看>>
offsetleft
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
mysql5.6的安装(rpm)
查看>>
Gamebryo实例学习之八InputDemo
查看>>
关于CSDN2013博客之星的一些看法
查看>>
"安全删除硬件并弹出媒体"的列表中出现内置硬盘的解决办法.
查看>>