0

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
            }
        ]
    }
  ]
}
EK_AllDay
  • 1,095
  • 2
  • 15
  • 35

3 Answers3

1

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;
}
Plato
  • 2,338
  • 18
  • 21
  • updated the answer, after seeing that you're getting an array of subjects from your server – Plato Mar 13 '15 at 22:42
  • I know the json has to be correct because I am building a android version of a ios application currently live on the app store. I get this error . FATAL EXCEPTION: main com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 136 path $[0].Courses[0].CourseName I read somewhere I need to setLenient to true. But that was for a JsonReader and I didnt know how to implement it – EK_AllDay Mar 13 '15 at 22:46
  • check this post, sounds like what you are talking about http://stackoverflow.com/questions/11484353/gson-throws-malformedjsonexception – Plato Mar 13 '15 at 22:53
  • never had to use the "lenient" feature in the past, but after reading the docs https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean), I think it's a good idea to let the people on your server side know, about this behaviour. The server's response is a bit dodgy, if you have to ask the reader to be lenient to parse it. – Plato Mar 13 '15 at 22:58
  • also giving us some feedback about what worked and what not, might be useful for others that visit this in he future ;) – Plato Mar 14 '15 at 21:39
  • Im still working on it, I just took a break over the weekend, Ill let you know what happens – EK_AllDay Mar 15 '15 at 19:53
1

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.

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
  • The json is valid, i just printed out the json by doing Log.d( result.get("StudentSubject").toString()); – EK_AllDay Mar 13 '15 at 22:32
1

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!!!

Shrikant Havale
  • 1,250
  • 1
  • 16
  • 36