您好,登錄后才能下訂單哦!
這篇“C#怎么基于JsonConvert解析Json數據”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“C#怎么基于JsonConvert解析Json數據”文章吧。
private void ParseJson() { // 解析為JObject string jsonStr = "{'name': 'zhangsan', 'sex': 'male', 'age': 23}"; JObject jo = JsonConvert.DeserializeObject<JObject>(jsonStr); print("name=" + jo["name"] + ", sex=" + jo["sex"] + ", age=" + jo["age"]); }
說明:需要使用以下命名空間:
using Newtonsoft.Json; using Newtonsoft.Json.Linq;
private void ParseJson() { // 解析為Dictionary<string, object> string jsonStr = "{'name': 'zhangsan', 'sex': 'male', 'age': 23}"; Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonStr); print("name=" + dict["name"] + ", sex=" + dict["sex"] + ", age=" + dict["age"]); }
class Student { public string name; public string sex; public int age; public override string ToString() { return "{name: " + name + ", sex: " + sex + ", age: " + age + "}"; } } private void ParseJson() { // 解析為Student string jsonStr = "{'name': 'zhangsan', 'sex': 'male', 'age': 23}"; Student stu = JsonConvert.DeserializeObject<Student>(jsonStr); print("stu=" + stu); }
private void ParseJson() { // 解析一維數組(JArray) string jsonStr = "[1, 2, 3, 4, 5]"; JArray ja = JsonConvert.DeserializeObject<JArray>(jsonStr); print(GetJArrayString(ja)); } private string GetJArrayString(JArray ja) { string str = "["; if (ja != null && ja.Count > 0) { str += ja[0]; } for (int i = 1; i < ja.Count; i++) { str += ", " + ja[i]; } return str + "]"; }
private void ParseJson() { // 解析一維數組(int[]) string jsonStr = "[1, 2, 3, 4, 5]"; int[] arr = JsonConvert.DeserializeObject<int[]>(jsonStr); print(GetArrString(arr)); } private string GetArrString(int[] arr) { string str = "["; if (arr != null && arr.Length > 0) { str += arr[0]; } for (int i = 1; i < arr.Length; i++) { str += ", " + arr[i]; } return str + "]"; }
private void ParseJson() { // 解析一維數組(List<int>) string jsonStr = "[1, 2, 3, 4, 5]"; List<int> list = JsonConvert.DeserializeObject<List<int>>(jsonStr); print(GetListString(list)); } private string GetListString(List<int> list) { string str = "["; if (list != null && list.Count > 0) { str += list[0]; } for (int i = 1; i < list.Count; i++) { str += ", " + list[i]; } return str + "]"; }
private void ParseJson() { // 解析二維數組(JArray) string jsonStr = "[[1, 2], [3, 4], [5, 6]]"; JArray ja2 = JsonConvert.DeserializeObject<JArray>(jsonStr); print(GetJArrayString2(ja2)); } private string GetJArrayString2(JArray ja2) { string str = "["; if (ja2 != null && ja2.Count > 0) { str += GetJArrayString((JArray)ja2[0]); } for (int i = 1; i < ja2.Count; i++) { str += ", " + GetJArrayString((JArray)ja2[i]); } return str + "]"; }
說明:GetJArrayString 方法見 2 1)節。
2)解析為二維數組
private void ParseJson() { // 解析二維數組(int[][]) string jsonStr = "[[1, 2], [3, 4], [5, 6]]"; int[][] arr = JsonConvert.DeserializeObject<int[][]>(jsonStr); print(GetArrString2(arr)); } private string GetArrString2(int[][] arr) { string str = "["; if (arr != null && arr.Length > 0) { str += GetArrString(arr[0]); } for (int i = 1; i < arr.Length; i++) { str += ", " + GetArrString(arr[i]); } return str + "]"; }
說明:GetArrString 方法見 2 2)節。
private void ParseJson() { // "數組字典"混合解析(JObject-JArray) string jsonStr = "{'vec1': [1, 2, 3, 4], 'vec2': [[1, 2], [3, 4], [5, 6]]}"; JObject jo = JsonConvert.DeserializeObject<JObject>(jsonStr); print("vec1=" + GetJArrayString((JArray) jo["vec1"]) + ", vec2=" + GetJArrayString2((JArray) jo["vec2"])); }
說明: GetJArrayString 方法見 2 1)節,GetJArrayString2 方法見 3 1)節。
class VecInfo { public int[] vec1; public int[][] vec2; } private void ParseJson() { // "數組字典"混合解析(封裝類) string jsonStr = "{'vec1': [1, 2, 3, 4], 'vec2': [[1, 2], [3, 4], [5, 6]]}"; VecInfo vecInfo = JsonConvert.DeserializeObject<VecInfo>(jsonStr); print("vec1=" + GetArrString(vecInfo.vec1) + ", vec2=" + GetArrString2(vecInfo.vec2)); }
說明: GetArrString 方法見 2 2)節,GetArrString2 方法見 3 2)節。
private void ParseJson() { // "字典數組"混合解析(JArray-JObject) string jsonStr = "[{'name': 'xiaomin', 'sex': 'male', 'age': 23}, {'name': 'xiaohong', 'sex': 'female', 'age': 21}]"; JArray ja = JsonConvert.DeserializeObject<JArray>(jsonStr); string str = "["; if (ja.Count > 0) { str += "{name: " + ja[0]["name"] + ", sex: " + ja[0]["sex"] + ", age: " + ja[0]["age"] + "}"; } for (int i = 1; i < ja.Count; i++) { str += ", {name: " + ja[i]["name"] + ", sex: " + ja[i]["sex"] + ", age: " + ja[i]["age"] + "}"; } str += "]"; print(str); }
private void ParseJson() { // "字典數組"混合解析(Student[]) string jsonStr = "[{'name': 'xiaomin', 'sex': 'male', 'age': 23}, {'name': 'xiaohong', 'sex': 'female', 'age': 21}]"; Student[] stus = JsonConvert.DeserializeObject<Student[]>(jsonStr); print(GetStudentArrString(stus)); } private string GetStudentArrString(Student[] stus) { string str = "["; if (stus != null && stus.Length > 0) { str += stus[0]; } for (int i = 1; i < stus.Length; i++) { str += ", " + stus[i]; } return str + "]"; }
說明:Student 類見 1 3)節。
private void ParseJson() { // "字典數組"混合解析(List<Student>) string jsonStr = "[{'name': 'xiaomin', 'sex': 'male', 'age': 23}, {'name': 'xiaohong', 'sex': 'female', 'age': 21}]"; List<Student> list = JsonConvert.DeserializeObject<List<Student>>(jsonStr); print(GetStudentListString(list)); } private string GetStudentListString(List<Student> list) { string str = "["; if (list != null && list.Count > 0) { str += list[0]; } for (int i = 1; i < list.Count; i++) { str += ", " + list[i]; } return str + "]"; }
private void ParseJson() { string jsonStr = "[{'x': 1, 'y': 2, 'z': 3}, {'x': 4, 'y': 5, 'z': 6}, {'x': 7, 'y': 8, 'z': 9}]"; Vector3[] points = JsonConvert.DeserializeObject<Vector3[]>(jsonStr); print(GetVectorString(points)); } private string GetVectorString(Vector3[] points) { string str = "["; if (points != null && points.Length > 0) { str += points[0]; } for (int i = 1; i < points.Length; i++) { str += ", " + points[i]; } return str + "]"; }
2)數組格式點
private void ParseJson() { string jsonStr = "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"; float[][] arr = JsonConvert.DeserializeObject<float[][]>(jsonStr); Vector3[] points = new Vector3[arr.Length]; for(int i = 0; i < arr.Length; i++) { points[i] = new Vector3(arr[i][0], arr[i][1], arr[i][2]); } print(GetVectorString(points)); }
說明:GetVectorString 方法同第 1)節。
private void ParseJson() { // 解析為JObject string jsonStr = File.ReadAllText("Assets/Scenes/Student.json", Encoding.UTF8); JObject jo = JsonConvert.DeserializeObject<JObject>(jsonStr); print("name=" + jo["name"] + ", sex=" + jo["sex"] + ", age=" + jo["age"]); }
說明:File 和 Encoding.UTF8 需要引入命名空間 System.IO、System.Text。
Student.json
{'name': 'zhangsan', 'sex': 'male', 'age': 23}
以上就是關于“C#怎么基于JsonConvert解析Json數據”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。