Newtonsoft.Json之JArray, JObject, JProperty,JValue
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"][0].Value<string>("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