-1

I have this JSON response that I want to get converted into java.

{"id":"4T4446Pj","count":4,"feasible":true,"route":{"0":{"name":"The Hague, The Netherlands","arrival":0,"distance":0},"1":{"name":"The Hague, The Netherlands","arrival":5,"distance":3.9},"2":{"name":"Uden, The Netherlands","arrival":93,"distance":137.1},"3":{"name":"Sint-Oedenrode, The Netherlands","arrival":112,"distance":160.5}}}

I've tried online converters, but they always result in different classes, where one of the classes has distinct attributes for (0,1,2,3).

public class Application {
  private String id;
  private float count;
  private boolean feasible;
  Route RouteObject;

 // Getter Methods 
 // Setter Methods 
}

public class Route {
  0 0Object;
  1 1Object;
  2 2Object;
  3 3Object;

 // Getter Methods 
 // Setter Methods 
}

public class 1 {
  private String name;
  private float arrival;
  private float distance;
 // Getter Methods 
 // Setter Methods 
}
public class 0 {
  private String name;
  private float arrival;
  private float distance;
 // Getter Methods 
 // Setter Methods 
} //the same for class 2 and 3

The number of locations is not fixed, so I want the locations to be an array or ArrayList. How can I do that?

Sara K
  • 3
  • 1

1 Answers1

0

Make sure you understand the difference between classes and objects. In order to use the JSON in your Java code you have to

  1. Create Java classes which mimic the JSON structure
  2. 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.

Christoph
  • 386
  • 1
  • 4