Make sure you understand the difference between classes and objects.
In order to use the JSON in your Java code you have to
- Create Java classes which mimic the JSON structure
- Use a JSON parser like Jackson to create your Java object instances from JSON. This is also called "deserialization" or "unmarshalling".
Using Jackson, you can use annotations (@JsonProperty
) to identify those Java fields which are parsed from JSON.
Your root object may look like:
public class Application {
@JsonProperty // indicate that this field exists in the JSON structure
private String id;
@JsonProperty
private float count;
@JsonProperty
private boolean feasible;
@JsonProperty("route") // indicates that the field name in the JSON is different from the field name in JAVA
private Map<Integer, Route> routeMap;
public String getId() {
return id;
}
public float getCount() {
return count;
}
public boolean isFeasible() {
return feasible;
}
public Map<Integer, Route> getRouteMap() {
return routeMap;
}
}
The class representing the routes may look like:
public class Route {
@JsonProperty
private String name;
@JsonProperty
private float arrival;
@JsonProperty
private float distance;
public String getName() {
return name;
}
public float getArrival() {
return arrival;
}
public float getDistance() {
return distance;
}
}
You can now use a Jackson ObjectMapper
to parse the JSON into Java:
// place your JSON in this string
String json = "{\"id\":\"4T4446Pj\",\"count\":4,...";
// create Jackson JSON <=> Object mapper
ObjectMapper objectMapper = new ObjectMapper();
// parse the json into the object
Application parsedObject = objectMapper.readValue(json, Application.class);
System.out.println("id: " + parsedObject.getId());
System.out.println("count: " + parsedObject.getCount());
System.out.println("feasible: " + parsedObject.isFeasible());
System.out.println("Number of routes: " + parsedObject.getRouteMap().size());
System.out.println("Routes:");
for (Map.Entry<Integer, Route> entry : parsedObject.getRouteMap().entrySet()) {
Integer index = entry.getKey();
Route route = entry.getValue();
System.out.println("\tRoute Index: " + index);
System.out.println("\tName: " + route.getName());
System.out.println("\tArrival: " + route.getArrival());
System.out.println("\tDistance: " + route.getDistance());
System.out.println("\t----------------------------------------------");
}
This code will generate the output:
id: 4T4446Pj
count: 4.0
feasible: true
Number of routes: 4
Routes:
Route Index: 0
Name: The Hague, The Netherlands
Arrival: 0.0
Distance: 0.0
----------------------------------------------
Route Index: 1
Name: The Hague, The Netherlands
Arrival: 5.0
Distance: 3.9
----------------------------------------------
Route Index: 2
Name: Uden, The Netherlands
Arrival: 93.0
Distance: 137.1
----------------------------------------------
Route Index: 3
Name: Sint-Oedenrode, The Netherlands
Arrival: 112.0
Distance: 160.5
----------------------------------------------
Jackson is not the only library for JSON to Java parsing. But they all share a similar concept.
There are a lot of annotations beyond @JsonProperty
to handle issues like ignoring a field, custom parsers or polymorphic type identification.