Newtonsoft.Json之JArray, JObject, JPropertyJValue
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<string>("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<int>("yy");
}
string yy1 = json["arr"][].Value<string>("yy");//也可以酱紫,多层的获取
string yy2 = json["arr"][]["yy"] != null ? json["arr"][]["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
Newtonsoft.Json之JArray, JObject, JPropertyJValue的更多相关文章
- Newtonsoft.Json之JArray, JObject, JProperty,JValue
JObject staff = new JObject(); staff.Add(new JProperty("Name", "Jack")); staff.A ...
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)
在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...
- Newtonsoft.Json解析json字符串和写json字符串
写: StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw); //如果报错则使用JsonWriter ...
- Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!
using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...
- Newtonsoft.Json解析数组
以下是解析json数组: var jsonInfo=[{"name":"abc","id":"1","coun ...
- C#利用newtonsoft.json读取.so配置文件内容
今天花 了点时间来使用 C#读取json文件 ,文件后缀为 .so文件 ,也是基于文件流的形式 获取 对象 ,然后解析; 之所以尝试 使用 json读取 ,是因为其配置文件的格式 更为友好 和方便,直 ...
- 遍历Newtonsoft.Json.Linq.JObject
JObject 遍历: 引用命名空间:using Newtonsoft.Json.Linq; JObject _jObject = JObject.Parse("{'ID':'001','M ...
- C# Newtonsoft.Json JObject移除属性,在序列化时忽略
原文 C# Newtonsoft.Json JObject移除属性,在序列化时忽略 一.针对 单个 对象移除属性,序列化时忽略处理 JObject实例的 Remove() 方法,可以在 指定序列化时移 ...
- C# Newtonsoft.Json JObject 操作
C# Newtonsoft.Json JObject 操作举例 JArray j = new JArray(); JObject obj = new JObject( ") ); JObje ...
随机推荐
- ASP.NET发布网站遇到的几个问题
环境背景 操作系统:Win7 开发工具:Visual Studio 2013 Framework: Framework 4.0 Issue1: 无法识别的属性“targetFramework” 解决方 ...
- Linux查看BIOS信息
http://www.linuxde.net/2013/02/12499.html
- 【ubuntu】中文输入法安装二三事
本来很愉快地刷着JS程序,很有感慨啊,想写篇博客记一下学习笔记,结果忘记了博客账号,后来通过邮箱找回了之后想要开始写..发现ubuntu的中文输入法不能用啊(其实不是不能用,就是小白没搞清楚状况,双系 ...
- hdu 5800 To My Girlfriend + dp
传送门:hdu 5800 To My Girlfriend 题意:给定n个物品,其中i,j必选,l,m必不选,问组成体积为s的方法一共有多少种 思路:定义dp[i][j][s1][s2],表示前i种物 ...
- 给Testerhome测试小道消息做个硬广告
测试小道消息在荔枝FM上的粉丝马上就要超过1k了.还差17个我们就能够开通社区了.欢迎大家都来收听测试小道消息哈--更多精彩内容还希望大家下载荔枝fm.关注FM245329. 如果你还不了解测试小道消 ...
- Android Studio运行程序出现Session ‘app’: Error Launching activity 解决办法
session "app":error launching activity 一下两种方法,可以轻松解决: 1. 2.把复选框去除:
- nginx 一二事(2) - 创建虚拟静态服务器
一.什么是nginx 是一个C语言开发的HTTP反向代理服务器,性能非常高 一个俄罗斯的哥们开发的,官方提供的测试性能能够达到5W的并发,我的天呐~,实际测试差不多是2W,而淘宝的牛人可以优化到200 ...
- 【hibernate】<第一节>hibernate简单入门
所需工具: ide:eclipse or myeclipse jdk:1.7 jar包:hibernate-distribution-3.6.0.Final 和对应mysql的驱动类(对应jar包如图 ...
- (转)轻量级JS焦点图/轮换图myFocus V2源码下载及安装教程
myFocus是一个专注于焦点图/轮换图制作的JS库,它小巧而且是完全独立的JS库,用它可以轻松的制作出网上绝大部分常见的焦点图(甚至包括flash焦点图),而且制作出的焦点图体积也非常的小(1KB左 ...
- 不支持一个 STA 线程上针对多个句柄的 WaitAll
[csharp] view plaincopy using System; using System.Collections.Generic; using System.Windows.Forms; ...