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
            }
        ]
    }
  ]
}
1
  • 1
    public ArrayList Courses; should be public ArrayList<Course> Courses; Commented Mar 13, 2015 at 22:10

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The json is valid, i just printed out the json by doing Log.d( result.get("StudentSubject").toString());
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;
}

6 Comments

updated the answer, after seeing that you're getting an array of subjects from your server
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
check this post, sounds like what you are talking about stackoverflow.com/questions/11484353/…
never had to use the "lenient" feature in the past, but after reading the docs google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/…, 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.
also giving us some feedback about what worked and what not, might be useful for others that visit this in he future ;)
|
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!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.