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

我从服务器收到了一个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
            }
        ]
    }
  ]
}

最佳答案

您的json根本无法验证。

标签必须在" "中,所有=必须在:

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

您尝试对其进行的任何json解析自然都会失败。

关于java - 将Gson数组转换为Arraylist,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29042809/

10-10 06:22