I'm new to JSON and I have trouble converting JSON to Java object. Here is the JSON I want to convert.
{
"John" : {
"fullname" : "John Wick",
"address" : "New York",
"status" : "Active",
"grades" : {
"physics" : 80,
"calculus" : 70,
"biology" : 85
}
},
"Indie" : {
"fullname" : "Indiana Jones",
"address" : "Los Angeles",
"status" : "On Leave",
"grades" : {
"physics" : 75,
"calculus" : 95,
"biology" : 65
}
},
"Gerard" : {
"fullname" : "Gerard Butler",
"address" : "San Fransisco",
"status" : "Non Active",
"grades" : {
"physics" : 0,
"calculus" : 0,
"biology" : 0
}
}
}
This the class that I have made. I might be wrong but is it okay to use the HashMap to map the grades key in the JSON?
public class Student {
private long id;
private String name;
private String address;
private String status;
private HashMap<String, Integer> score;
public Siswa(String name, String address, String status, HashMap<String, Integer> score) {
this.score = score;
this.name = name;
this.address = address;
this.status = status;
}
public Student(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return this.address;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatus() {
return this.status;
}
public HashMap<String, Integer> getScore() {
return score;
}
public void setNilai(HashMap<String, Integer> score) {
this.score = score;
}
}
class Score {
private int id_score;
private int phyGrade;
private int calGrade;
private int bioGrade;
public Nilai(int id_score, int phyGrade, int calGrade, int bioGrade) {
this.id_score = id_score;
this.phyGrade = phyGrade;
this.calGrade = calGrade;
this.bioGrade = bioGrade;
}
public int getId_score() {
return id_score;
}
public void setId_nilai(int id_nilai) {
this.id_score = id_score;
}
public void setPhyGrade(int phy) {
this.phyGrade = phy;
}
public int getPhyGrade() {
return this.phyGrade;
}
public void setCalGrade(int cal) {
this.calGrade = cal;
}
public int getCalGrade() {
return this.calGrade;
}
public void setBioGrade(int bio) {
this.bioGrade = bio;
}
public int getBioGrade() {
return this.bioGrade;
}
}
I want to retrieve each key:value and convert them to Java object with the class I have made. Any idea how to resolve this?