How do I use json to create a custom object?

96 views Asked by At

Real title: How do I convert json to custom object using gson(custom object contains ArrayLists and HashMap)?

Problem: I added an HashMap to my custom object and since then when im trying to convert JSON to my custom object I get this error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 59 path $[0].currentLesson.students.

What do I need to change? ask for any other info you might need from me and I shall give it to you. Thank you!

Code:

private void initializeDatabase() {
        ArrayList<Group> groups = null;

        SharedPreferences sharedPreferences =  getSharedPreferences(Database.SHARED_PREFERENCES_STRING, MODE_PRIVATE);

        Gson gson = new Gson();
        String groupsJason = sharedPreferences.getString(Database.GROUPS_STRING, null);

        Type typeGroup = new TypeToken<ArrayList<Group>>(){}.getType();
        groups = gson.fromJson(groupsJason, typeGroup);

        if(groups == null){
            groups = new ArrayList<>();
        }

        Database.setGroups(groups);
    }

public class Group {

    private String groupName;

    private ArrayList<Student> students;
    private ArrayList<Lesson> lessons;
    private Lesson currentLesson;


    public Group(String groupName) {
        this.groupName = groupName;
        students = new ArrayList<>();
        lessons = new ArrayList<>();
    }

    public Group(String groupName, ArrayList<Student> students) {
        this.groupName = groupName;
        this.students = students;
        lessons = new ArrayList<>();
    }

    public void setCurrentLesson(String currentLesson) {
        this.currentLesson = new Lesson(currentLesson, students);
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public ArrayList<Student> getStudents() {
        return students;
    }

    public int getGroupSize() {
        return students.size();
    }

    public Boolean getArrivedToLesson(Student student){
        return currentLesson.getArrivedToLesson(student);
    }

    public Lesson getCurrentLesson() {
        return currentLesson;
    }

    public void saveLesson() {
        lessons.add(currentLesson);
    }

}

public class Lesson {

    private String lessonDate;

    private HashMap<Student, Boolean> students;

    public Lesson(String lessonDate, ArrayList<Student> students) {
        this.lessonDate = lessonDate;
        this.students = new HashMap<>();

        for (Student student : students) {
            this.students.put(student, false);
        }
    }
    
    public String getLessonDate() {
        return lessonDate;
    }

    public void arrivedToLesson(Student student) {
        student.arrivedToLesson();
        students.put(student, true);
    }

    public void didntArriveToLesson(Student student) {
        student.didntArriveToLesson();
        students.put(student, false);
    }

    public Boolean getArrivedToLesson(Student student) {
        return students.get(student);
    }
}

private void saveData(){

        group.saveLesson();

        SharedPreferences sharedPreferences = getSharedPreferences(Database.SHARED_PREFERENCES_STRING, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        Gson gson = new Gson();
        String groupsJason = gson.toJson(Database.getGroups());

        editor.putString(Database.GROUPS_STRING, groupsJason);
        editor.apply();

        Toast.makeText(this, String.format(getResources().getString(R.string.saved_attendance), lessonDate), Toast.LENGTH_SHORT).show();
        onButtonBackClick();
    }
  
1

There are 1 answers

0
Elad Kellner On

I've managed to solve the problem. The problem was that my map - I changed it to HashMap<String, Object>. The string represents my custom object's main attribute - in my case it's name, the object is a boolean and I casts it when needed.