Create java Pojos and annotate with Jackson 2
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "Search" })
public class Bike {
@JsonProperty("Search")
private Search search;
/**
* No args constructor for use in serialization
*
*/
public Bike() {
}
/**
*
* @param search
*/
public Bike(final Search search) {
super();
this.search = search;
}
@JsonProperty("Search")
public Search getSearch() {
return search;
}
@JsonProperty("Search")
public void setSearch(final Search search) {
this.search = search;
}
@Override
public String toString() {
return "Bike [search=" + search + "]";
}
}
package com.example;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "BikeList" })
public class Search {
@JsonProperty("BikeList")
private List<BikeList> bikeList = null;
/**
* No args constructor for use in serialization
*
*/
public Search() {
}
/**
*
* @param bikeList
*/
public Search(final List<BikeList> bikeList) {
super();
this.bikeList = bikeList;
}
@JsonProperty("BikeList")
public List<BikeList> getBikeList() {
return bikeList;
}
@JsonProperty("BikeList")
public void setBikeList(final List<BikeList> bikeList) {
this.bikeList = bikeList;
}
@Override
public String toString() {
return "Search [bikeList=" + bikeList + "]";
}
}
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "weight", "colour", "price", "name" })
public class BikeList {
@JsonProperty("weight")
private String weight;
@JsonProperty("colour")
private String colour;
@JsonProperty("price")
private Double price;
@JsonProperty("name")
private String name;
/**
* No args constructor for use in serialization
*
*/
public BikeList() {
}
/**
*
* @param colour
* @param price
* @param weight
* @param name
*/
public BikeList(final String weight, final String colour, final Double price, final String name) {
super();
this.weight = weight;
this.colour = colour;
this.price = price;
this.name = name;
}
@JsonProperty("weight")
public String getWeight() {
return weight;
}
@JsonProperty("weight")
public void setWeight(final String weight) {
this.weight = weight;
}
@JsonProperty("colour")
public String getColour() {
return colour;
}
@JsonProperty("colour")
public void setColour(final String colour) {
this.colour = colour;
}
@JsonProperty("price")
public Double getPrice() {
return price;
}
@JsonProperty("price")
public void setPrice(final Double price) {
this.price = price;
}
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(final String name) {
this.name = name;
}
@Override
public String toString() {
return "BikeList [weight=" + weight + ", colour=" + colour + ", price=" + price + ", name=" + name + "]";
}
}
Then employ Jackson to read input json and convert to Java Objects
package com.example;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
public class Stackoverflow {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final ObjectReader OBJECT_READER_BIKE = OBJECT_MAPPER.readerFor(Bike.class);
public static void main(final String[] args) throws IOException {
final Bike bike = OBJECT_READER_BIKE.readValue(new File("input/bike.json"));
System.out.println(bike);
}
}
output obtained:-
Bike [search=Search [bikeList=[BikeList [weight=14.8, colour=Blue, price=149.99, name=Hybrid Pro], BikeList [weight=15.8, colour=Red, price=249.99, name=Slant comp], BikeList [weight=17.9, colour=Pink, price=500.0, name=Charm]]]]