Author Archives: Nikola Milikic

Example of iterating through JSON attributes in Gson

When working with Gson, in case of a JSON object having too many attributes or those attributes not being known upfront, it is not practical to (try to) convert the JSON object to a Java object (via fromJson() method). This situation will occur when you start working on your JSON assignment. The assignment includes calling Currency Converter API service that retrieves a list of countries. The service retrieves the total of 201 countries, but the developers of this API, for a reason unclear to me, decided to store the list of countries not as a JSON array, but as attributes of a JSON object. However, there is a simple approach to parsing countries represented in this form.

First, like we did during the class, we need to fetch the content from this REST service, convert it to a JSON object and get the value of the results attribute. The following example shows how to parse countries from the JSON object that is the value of the results attribute. This object is called countriesJson.

JsonObject countriesJson = ...
Set<Entry<String, JsonElement>> attributeEntres = countriesJson.entrySet();

for (Entry<String, JsonElement> entry : countriesJson.entrySet()) {
    String attributeName = entry.getKey();
    JsonObject attributeValue = (JsonObject) entry.getValue();


Method entrySet() retrieves an object attributeEntres of a class Set. Class Set represents a set of elements, i.e. an unordered collection of elements in Java (compared to a list that represents an ordered collection of elements). A set contains objects of a class Entry that has two fields: key and value. In our case, each Entry instance represents a JSON attribute (corresponding to a country). The key is the name of a JSON attribute (country code), and the value is the value of the JSON attribute (which is a JSON object holding information about the country).

Since set is an unordered collection, we can not access elements via an index (like we do with lists by using the get() method). One approach to iterating over a set is by using the for-each loop. For-each loop performs similar operation as the for loop does, with an important difference where for-each loop iterates throughout the whole collection (unless explicitly interrupted via break). A short description of the for-each loop can be found here. In each iteration, entry references an object being accessed in the current iteration. In our example, by using the getKey() method we can fetch the JSON attribute name (country code), and by invoking the getValue() method we can fetch the value of the JSON attribute. The method getValue() returns a reference of the class JsonElement. As depicted in the presentation (page 16), class JsonElement is a parent class of  JsonPrimitive (representing a literal, such as a number, string, boolean, etc.), JsonObject, JsonArray and JsonNull. In our case, since we know the value of each attribute of the countriesJson is a JSON object, we cast it to JsonObject class.

JSON task is published

Task for the JSON badge has been published. Deadline for completing the task is 01.06.2017.

Lab Classes Schedule

The advanced course will be taught according to the following schedule:

  • Tuesday 12:15-14:00h – Groups B1, B4 – Room A106
  • Thursday 14:15-16:00h – Groups B2, B3, B5 – Room A003

Website is launched

The website for The Advanced Course of the Principles of Programming has been launched. Here students can find all necessary information about specific topics that will be taught during the course. Also, details about specific assignments and tasks will be posted on this website.

This website is powered with the BadgeOS badging platform. The idea is to award students with specific badges representing each topic. A student can achieve a badge by completing an assignment specified for that topic.