1、JObject:基本的json對象
/// <summary> /// Gets the j object. /// </summary> /// <returns></returns> public JObject GetJObject() { var obj = new JObject {{"Name", "Mark" } }; return obj; }
2、JObject:嵌套子物件(JObject嵌JObject)
/// <summary> /// Gets the j object. /// </summary> /// <returns></returns> public JObject GetJObject() { var obj = new JObject {{"Name", "Mark"}, {"Age", 8 }}; var info = new JObject {{"Phone", "132****7777"}, {"Gender", "男"}}; obj.Add("Info", info); return obj; }
3、JArray:基本json對象中的數組
/// <summary> /// Gets the j array. /// </summary> /// <returns></returns> public JArray GetJArray() { var jarray = new JArray(); var mark = new JObject { { "Name", "Mark" }, { "Age", 8 } }; var jack = new JObject { { "Name", "Jack" }, { "Age", 9 } }; jarray.Add(mark); jarray.Add(jack); return jarray; }
4、JArray: 多個json對象數組
/// <summary> /// Gets the j array. /// </summary> /// <returns></returns> public JObject GetJArray() { var obj = new JObject(); var student = new JArray { new JObject {{ "Name", "Mark" }, { "Age", 8 } }, new JObject {{ "Name", "Jack" }, { "Age", 9 } } }; var results = new JArray { new JObject {{ "Subject", "語文"}, { "Score", 100}}, new JObject {{ "Subject", "數學" }, { "Score", 88}} }; obj.Add("Student", student); obj.Add("Results", results); return obj; }
5、JArray:json數組嵌套數組(一個學生對應多個課程分數)
/// <summary> /// Gets the results. /// </summary> /// <returns></returns> public JObject GetResults() { var mark = new JObject { { "Name", "Mark" }, { "Age", "8" } }; var results = new JArray { new JObject {{ "Subject", "語文"}, { "Score", 100}}, new JObject {{ "Subject", "數學" }, { "Score", 88}} }; mark.Add("Results", results); return mark; }
//--------------------------------------------------------------------------
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查詢後的結果 |
1.建立JSON對象
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.JArray類似。
方法 |
說明 |
JObject.Parse(string json) |
json含有JSON對象的字串,返回為JObject對象 |
JObject.FromObject(object o) |
o為要轉化的對象,返回一個JObject對象 |
JObject.Load(JsonReader reader) |
reader包含著JSON對象的內容,返回一個JObject對象 |
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());
結果:
三.使用Linq to JSON
1.查詢
首先準備Json字串,是一個包含員工基本資料的Json
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()"可以返回所有數組中的對象
結果: