• 【C#】基于JsonConvert解析Json数据


    1 解析字典

            1)解析为 JObject

    1. private void ParseJson() { // 解析为JObject
    2. string jsonStr = "{'name': 'zhangsan', 'sex': 'male', 'age': 23}";
    3. JObject jo = JsonConvert.DeserializeObject(jsonStr);
    4. print("name=" + jo["name"] + ", sex=" + jo["sex"] + ", age=" + jo["age"]);
    5. }

            说明:需要使用以下命名空间:

    1. using Newtonsoft.Json;
    2. using Newtonsoft.Json.Linq;

            2)解析为 Dictionary

    1. private void ParseJson() { // 解析为Dictionary
    2. string jsonStr = "{'name': 'zhangsan', 'sex': 'male', 'age': 23}";
    3. Dictionary<string, object> dict = JsonConvert.DeserializeObjectstring, object>>(jsonStr);
    4. print("name=" + dict["name"] + ", sex=" + dict["sex"] + ", age=" + dict["age"]);
    5. }

             3)解析为封装类

    1. class Student {
    2. public string name;
    3. public string sex;
    4. public int age;
    5. public override string ToString() {
    6. return "{name: " + name + ", sex: " + sex + ", age: " + age + "}";
    7. }
    8. }
    9. private void ParseJson() { // 解析为Student
    10. string jsonStr = "{'name': 'zhangsan', 'sex': 'male', 'age': 23}";
    11. Student stu = JsonConvert.DeserializeObject(jsonStr);
    12. print("stu=" + stu);
    13. }

    2 解析一维数组

             1)解析为 JArray

    1. private void ParseJson() { // 解析一维数组(JArray)
    2. string jsonStr = "[1, 2, 3, 4, 5]";
    3. JArray ja = JsonConvert.DeserializeObject(jsonStr);
    4. print(GetJArrayString(ja));
    5. }
    6. private string GetJArrayString(JArray ja) {
    7. string str = "[";
    8. if (ja != null && ja.Count > 0) {
    9. str += ja[0];
    10. }
    11. for (int i = 1; i < ja.Count; i++) {
    12. str += ", " + ja[i];
    13. }
    14. return str + "]";
    15. }

            2)解析为数组

    1. private void ParseJson() { // 解析一维数组(int[])
    2. string jsonStr = "[1, 2, 3, 4, 5]";
    3. int[] arr = JsonConvert.DeserializeObject<int[]>(jsonStr);
    4. print(GetArrString(arr));
    5. }
    6. private string GetArrString(int[] arr) {
    7. string str = "[";
    8. if (arr != null && arr.Length > 0) {
    9. str += arr[0];
    10. }
    11. for (int i = 1; i < arr.Length; i++) {
    12. str += ", " + arr[i];
    13. }
    14. return str + "]";
    15. }

            3)解析为列表

    1. private void ParseJson() { // 解析一维数组(List)
    2. string jsonStr = "[1, 2, 3, 4, 5]";
    3. List<int> list = JsonConvert.DeserializeObjectint>>(jsonStr);
    4. print(GetListString(list));
    5. }
    6. private string GetListString(List<int> list) {
    7. string str = "[";
    8. if (list != null && list.Count > 0) {
    9. str += list[0];
    10. }
    11. for (int i = 1; i < list.Count; i++) {
    12. str += ", " + list[i];
    13. }
    14. return str + "]";
    15. }

    3 解析二维数组

             1)解析为 JArray

    1. private void ParseJson() { // 解析二维数组(JArray)
    2. string jsonStr = "[[1, 2], [3, 4], [5, 6]]";
    3. JArray ja2 = JsonConvert.DeserializeObject(jsonStr);
    4. print(GetJArrayString2(ja2));
    5. }
    6. private string GetJArrayString2(JArray ja2) {
    7. string str = "[";
    8. if (ja2 != null && ja2.Count > 0) {
    9. str += GetJArrayString((JArray)ja2[0]);
    10. }
    11. for (int i = 1; i < ja2.Count; i++) {
    12. str += ", " + GetJArrayString((JArray)ja2[i]);
    13. }
    14. return str + "]";
    15. }

            说明:GetJArrayString 方法见 2 1)节。 

             2)解析为二维数组

    1. private void ParseJson() { // 解析二维数组(int[][])
    2. string jsonStr = "[[1, 2], [3, 4], [5, 6]]";
    3. int[][] arr = JsonConvert.DeserializeObject<int[][]>(jsonStr);
    4. print(GetArrString2(arr));
    5. }
    6. private string GetArrString2(int[][] arr) {
    7. string str = "[";
    8. if (arr != null && arr.Length > 0) {
    9. str += GetArrString(arr[0]);
    10. }
    11. for (int i = 1; i < arr.Length; i++) {
    12. str += ", " + GetArrString(arr[i]);
    13. }
    14. return str + "]";
    15. }

            说明:GetArrString 方法见 2 2)节。

    4 解析数组字典

            1)解析为 JObject-JArray

    1. private void ParseJson() { // "数组字典"混合解析(JObject-JArray)
    2. string jsonStr = "{'vec1': [1, 2, 3, 4], 'vec2': [[1, 2], [3, 4], [5, 6]]}";
    3. JObject jo = JsonConvert.DeserializeObject(jsonStr);
    4. print("vec1=" + GetJArrayString((JArray) jo["vec1"]) + ", vec2=" + GetJArrayString2((JArray) jo["vec2"]));
    5. }

            说明: GetJArrayString 方法见 2 1)节,GetJArrayString2 方法见 3 1)节。

            2)解析为封装类

    1. class VecInfo {
    2. public int[] vec1;
    3. public int[][] vec2;
    4. }
    5. private void ParseJson() { // "数组字典"混合解析(封装类)
    6. string jsonStr = "{'vec1': [1, 2, 3, 4], 'vec2': [[1, 2], [3, 4], [5, 6]]}";
    7. VecInfo vecInfo = JsonConvert.DeserializeObject(jsonStr);
    8. print("vec1=" + GetArrString(vecInfo.vec1) + ", vec2=" + GetArrString2(vecInfo.vec2));
    9. }

            说明: GetArrString 方法见 2 2)节,GetArrString2 方法见 3 2)节。

    5 解析字典数组

            1)解析为 JArray-JObject

    1. private void ParseJson() { // "字典数组"混合解析(JArray-JObject)
    2. string jsonStr = "[{'name': 'xiaomin', 'sex': 'male', 'age': 23}, {'name': 'xiaohong', 'sex': 'female', 'age': 21}]";
    3. JArray ja = JsonConvert.DeserializeObject(jsonStr);
    4. string str = "[";
    5. if (ja.Count > 0) {
    6. str += "{name: " + ja[0]["name"] + ", sex: " + ja[0]["sex"] + ", age: " + ja[0]["age"] + "}";
    7. }
    8. for (int i = 1; i < ja.Count; i++) {
    9. str += ", {name: " + ja[i]["name"] + ", sex: " + ja[i]["sex"] + ", age: " + ja[i]["age"] + "}";
    10. }
    11. str += "]";
    12. print(str);
    13. }

             2)解析为数组封装类

    1. private void ParseJson() { // "字典数组"混合解析(Student[])
    2. string jsonStr = "[{'name': 'xiaomin', 'sex': 'male', 'age': 23}, {'name': 'xiaohong', 'sex': 'female', 'age': 21}]";
    3. Student[] stus = JsonConvert.DeserializeObject(jsonStr);
    4. print(GetStudentArrString(stus));
    5. }
    6. private string GetStudentArrString(Student[] stus) {
    7. string str = "[";
    8. if (stus != null && stus.Length > 0) {
    9. str += stus[0];
    10. }
    11. for (int i = 1; i < stus.Length; i++) {
    12. str += ", " + stus[i];
    13. }
    14. return str + "]";
    15. }

            说明:Student 类见 1 3)节。

             3)解析为列表封装类

    1. private void ParseJson() { // "字典数组"混合解析(List)
    2. string jsonStr = "[{'name': 'xiaomin', 'sex': 'male', 'age': 23}, {'name': 'xiaohong', 'sex': 'female', 'age': 21}]";
    3. List list = JsonConvert.DeserializeObject>(jsonStr);
    4. print(GetStudentListString(list));
    5. }
    6. private string GetStudentListString(List list) {
    7. string str = "[";
    8. if (list != null && list.Count > 0) {
    9. str += list[0];
    10. }
    11. for (int i = 1; i < list.Count; i++) {
    12. str += ", " + list[i];
    13. }
    14. return str + "]";
    15. }

    6 解析 Vector3 点集

             1)字典格式点

    1. private void ParseJson() {
    2. string jsonStr = "[{'x': 1, 'y': 2, 'z': 3}, {'x': 4, 'y': 5, 'z': 6}, {'x': 7, 'y': 8, 'z': 9}]";
    3. Vector3[] points = JsonConvert.DeserializeObject(jsonStr);
    4. print(GetVectorString(points));
    5. }
    6. private string GetVectorString(Vector3[] points) {
    7. string str = "[";
    8. if (points != null && points.Length > 0) {
    9. str += points[0];
    10. }
    11. for (int i = 1; i < points.Length; i++) {
    12. str += ", " + points[i];
    13. }
    14. return str + "]";
    15. }

            2)数组格式点

    1. private void ParseJson() {
    2. string jsonStr = "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]";
    3. float[][] arr = JsonConvert.DeserializeObject<float[][]>(jsonStr);
    4. Vector3[] points = new Vector3[arr.Length];
    5. for(int i = 0; i < arr.Length; i++) {
    6. points[i] = new Vector3(arr[i][0], arr[i][1], arr[i][2]);
    7. }
    8. print(GetVectorString(points));
    9. }

            说明:GetVectorString 方法同第 1)节。 

    7 从文件中解析

    1. private void ParseJson() { // 解析为JObject
    2. string jsonStr = File.ReadAllText("Assets/Scenes/Student.json", Encoding.UTF8);
    3. JObject jo = JsonConvert.DeserializeObject(jsonStr);
    4. print("name=" + jo["name"] + ", sex=" + jo["sex"] + ", age=" + jo["age"]);
    5. }

            说明:File 和  Encoding.UTF8 需要引入命名空间 System.IO、System.Text。

            Student.json

    {'name': 'zhangsan', 'sex': 'male', 'age': 23}
  • 相关阅读:
    在公司项目中使用git的简单手册
    Python 常用的内置模块
    Python实现猎人猎物优化算法(HPO)优化随机森林分类模型(RandomForestClassifier算法)项目实战
    Java中使用JTS实现WKT字符串读取转换线、查找LineString的list中距离最近的线、LineString做缓冲区扩展并计算点在缓冲区内的方位角
    项目管理PMP6.0-五大过程组、十大知识领域、四十九个过程(记忆码:7664363734)
    服务器扩容步骤
    解决电脑显示找不到msvcp140_CODECVT_IDS.dll文件的办法
    TCP/UDP
    mybatis_plus条件构造器
    嵌入式系统,内存不够了该怎么办?
  • 原文地址:https://blog.csdn.net/m0_37602827/article/details/128155186