JObject,JArray的基本操做

引用:https://www.cnblogs.com/dacongge/p/6957074.html

一、JObject:基本的json對象

/// <summary> /// Gets the j object. /// </summary> /// <returns></returns> public JObject GetJObject() { var obj = new JObject {{"Name", "Mark" } }; return obj; }

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

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

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

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

相關文章
相關標籤/搜索