1

This has probably been asked before, but I do not know the terminology for my question, therefore do not know what to look up.

I am using GSON and Java trying to get information from a parsed JSONElement.

Java code:

    JsonParser parser = new JsonParser();

    String url = "https://chapel-logs.herokuapp.com/attendance";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("Accept", "application/json");

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }

    in.close();

    //print result
    JsonElement element = parser.parse(response.toString());

    if (element.isJsonObject()) {
        JsonObject albums = element.getAsJsonObject();
        System.out.println(albums.get("students")); //prints out data in students
        System.out.println(albums.get("students.LastChapelAttended")); //error
    }

My JSON :

{"students":[{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":6,"Hour":15,"Min":14,"Sec":28},"StudNum":"F02660934","Attendance":17},{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":5,"Hour":19,"Min":49,"Sec":11},"StudNum":"002660934","Attendance":2},{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":4,"Hour":20,"Min":35,"Sec":57},"StudNum":"002643472","Attendance":2},{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":7,"Hour":5,"Min":34,"Sec":54},"StudNum":"002664906","Attendance":1}]}

The data I am trying to get is: LastChapelAttended, however LastChapelAttended is within students. In JavaScript the equivalent to what I am trying would be students.LastChapelAttended if that helps.

Thanks in advance!

Seth Wheeler
  • 353
  • 1
  • 2
  • 16

4 Answers4

3
JsonObject jObj=(JsonObject)albums.get("students").getAsJsonArray().get(0);
System.out.println(jObj.get("LastChapelAttended"));

Get it as a JsonArray and then loop through the array to get LastChapelAttended.

Amstel D'Almeida
  • 630
  • 6
  • 13
1

First, students is not an object but an array. Thus students.LastChapelAttended should not work in any language. If you want to retrieve LastChapelAttended of the first student in the array students[0].LastChapelAttended should work.

I am not familiar with gson, but I think what you want to do is something like this:

if (element.isJsonObject()) {
    JsonObject albums = element.getAsJsonObject();
    JsonArray students = albums.getAsJsonArray("students");

    for (JsonElement student : students) {
        System.out.println(albums.getAsJsonObject().get("LastChapelAttended"));
    }
}
stevecross
  • 5,588
  • 7
  • 47
  • 85
1

You can check this out for help Difference between JSONObject and JSONArray

Students is a JsonArray LastChapelAttended is a JSONObject you can get it by calling

json = (json data)
JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(json).getAsJsonObject();
JsonArray studentsArray = rootObj.getAsJsonArray("students");
for (JsonElement stu : studentsArray) {
JsonObject student = stu.getAsJsonObject();
JsonObject LastChapelAttended = student.getAsJsonObject("LastChapelAttended");

}
Bolu Okunaiya
  • 129
  • 1
  • 10
0

Just iterate over the student array

albums.getAsJsonArray("students")
      .forEach(student -> System.out.println(student.getAsJsonObject()
                            .get("LastChapelAttended")));
Michal
  • 970
  • 7
  • 11