将 Gson 数组转换为 Arraylist

Convert Gson Array to Arraylist

我看到了很多这样的问题,但是我仍然无法解决这个问题。

我从我的服务器收到了一个 json,它有多个 gson 数组。

如何反序列化来自服务器的响应以满足这些模型。

主题模型:

public class Subject {  
    public int SubjectId;
    public String SubjectName;
    public ArrayList Courses;
}

课程模型:

public class Course {
    public String CourseName;
    public String CourseDescription;
    public int CourseId;
    public int Subject_SubjectId;
}

编辑 这是服务器返回的内容:我使用 google Chrome 扩展 PostMan 来检索它。

这是实际的

{
"StudentSubject": [
    {
        "SubjectId": 1059,
        "SubjectName": "Accounting",
        "Student_CourseId": 0,
        "UniversitySubjectId": 0,
        "Courses": [
            {
                "CourseId": 1091,
                "CourseName": "ACCT 101",
                "CourseDescription": "",
                "Subject_SubjectId": 1059
            },
            {
                "CourseId": 1092,
                "CourseName": "ACCT 111",
                "CourseDescription": "",
                "Subject_SubjectId": 1059
            },
            {
                "CourseId": 1093,
                "CourseName": "ACCT 115",
                "CourseDescription": "Financial Accounting Foundations",
                "Subject_SubjectId": 1059
            }
        ]
     },
    {
        "SubjectId": 1060,
        "SubjectName": "Mathematics",
        "Student_CourseId": 0,
        "UniversitySubjectId": 0,
        "Courses": [
            {
                "CourseId": 1094,
                "CourseName": "MATH 100",
                "CourseDescription": "Fundamentals of Mathematics",
                "Subject_SubjectId": 1060
            },
            {
                "CourseId": 1095,
                "CourseName": "MATH 101",
                "CourseDescription": "Introduction to Analysis I",
                "Subject_SubjectId": 1060
            },
            {
                "CourseId": 2126,
                "CourseName": "MATH 200",
                "CourseDescription": "Multivariate Calculus",
                "Subject_SubjectId": 1060
            },
            {
                "CourseId": 2132,
                "CourseName": "MATH 102",
                "CourseDescription": "Introduction to Analysis II",
                "Subject_SubjectId": 1060
            }
        ]
    },
    {
        "SubjectId": 1069,
        "SubjectName": "Bioscience & Biotechnology",
        "Student_CourseId": 0,
        "UniversitySubjectId": 0,
        "Courses": [
            {
                "CourseId": 1109,
                "CourseName": "BIO 100",
                "CourseDescription": "Applied Cells, Genetics & Physiology",
                "Subject_SubjectId": 1069
            },
            {
                "CourseId": 2123,
                "CourseName": "BIO 124",
                "CourseDescription": "Evolution & Organismal Diversity",
                "Subject_SubjectId": 1069
            }
        ]
    },
    {
        "SubjectId": 2084,
        "SubjectName": "Computer Science",
        "Student_CourseId": 0,
        "UniversitySubjectId": 0,
        "Courses": [
            {
                "CourseId": 2137,
                "CourseName": "CS 101",
                "CourseDescription": "",
                "Subject_SubjectId": 2084
            }
        ]
    },
    {
        "SubjectId": 2086,
        "SubjectName": "Business Statistics",
        "Student_CourseId": 0,
        "UniversitySubjectId": 0,
        "Courses": [
            {
                "CourseId": 2141,
                "CourseName": "STAT 101",
                "CourseDescription": "",
                "Subject_SubjectId": 2086
            }
        ]
    }
  ]
}

尝试

Type listType = new TypeToken<List<Subject>>() {}.getType();
List<Subject> subjects = new Gson().fromJson(yourJsonString, listType);

您的主题 class 应如下所示

class Subject {  
  int SubjectId;
  String SubjectName;
  List<Course> Courses;
}

您的 json 根本没有通过验证。

标签需要在" "=都需要在:

使用 http://www.jslint.com/ 检查其他错误。

任何json 尝试解析它自然会失败。

正如 Plato 所提到的,Json 字符串的格式不正确,这是我用来测试您想要实现的概念的正确格式。

[{"SubjectId":"1059.0",
  "SubjectName":"Accounting",
  "Student_CourseId":"0.0",
  "UniversitySubjectId":"0.0",
  "Courses":[
        {"CourseId":"1091.0",
         "CourseName":"ACCT 101",
         "CourseDescription":"",
         "Subject_SubjectId":"1059.0"
         },
         {"CourseId":"1092.0",
         "CourseName":"ACCT 111",
         "CourseDescription":"",
         "Subject_SubjectId":"1059.0"
         }
        ]
 }]

这个gson库可以使用后,

    Gson gson = new Gson();
    Subject[] subject = gson.fromJson(jsonString, Subject[].class);
    ArrayList<Course> course = subject[0].Courses;
    System.out.println(subject[0].SubjectId);
    System.out.println(course.get(0).CourseId);

当然,只有当 Json 的格式正确时,上述逻辑才会起作用,更正您从服务器收到的 json 响应,然后它应该起作用。

希望对您有所帮助!!!