简体   繁体   中英

Convert Gson Array to Arraylist

I have seen a ton of questions like this, however I have still been unable to resolve this issue.

I received a json from my server that has multiple gson arrays.

How can I deserialize my response from the server to satisfy these models.

Subject Model:

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

Course Model:

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

EDIT Here is what the server is returning: I used the google Chrome Extension PostMan to retrieve it.

Here is the actual

{
"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
            }
        ]
    }
  ]
}

Try

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

Where your Subject class should look like this

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

Your json doesn't validate at all.

The labels need to be in " " and the = all need to be :

Use http://www.jslint.com/ to check for other errors.

Any json parsing you attempt on it will naturally fail.

As mentioned by Plato, Json string is not in correct format, this is correct one I used to test the concept you want to achieve.

[{"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"
         }
        ]
 }]

After this gson library can be used,

    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);

ofcourse the above logic will work only if Json is in correct format, correct your json response that you are receiving from your server and then it should work.

Hope this helps!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM