您好,登录后才能下订单哦!
这篇“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。