I've this json object:
public static final String JSON_TEXT = "{" +
"\"Subjects\": [{" +
"\"primaryKey\": \"1\"," +
"\"subjectName\": \"English\"" +
"}," +
"{" +
"\"primaryKey\": \"2\"," +
"\"subjectName\": \"Spanish\"" +
"}" +
"]," +
"\"Exams\": [{" +
"\"primaryKey\": \"1\"," +
"\"grade\": \"10\"" +
"}," +
"{" +
"\"primaryKey\": \"2\"," +
"\"grade\": \"20\"" +
"}" +
"]" +
"}";
and need to build some methods for a json dao implementation.
I've built
public Collection<Subject> getSubjects()
successfully but I got totally stuck with
public Subject findSubjectById(Integer subjectId)
This was my first idea so far:
public Subject findSubjectById(Integer subjectId) {
JSONObject obj = (JSONObject)JSON_PARSER.parse(JSON_TEXT);
if (obj.get(subjectId) != null)
try {
JSONArray subjectsArray = (JSONArray) obj.get("Subjects");
for (int i = 0; i < subjectsArray.length(); i++){
}
} catch (ParseException e) {
e.printStackTrace();
}
return subject;
}
Any ideas/ examples are highly appreciated to solve this. Thanks in advance!