Jagdish Jagdish - 3 months ago 16
JSON Question

Jackson with JSON : receiving "null" object

I need to convert a Uber Products JSON response from here to a Java object. I am using Jackson for JSON handling. My input JSON looks like:

{
"products": [
{
"capacity": 2,
"description": "Ride for less with uberPOOL",
"price_details": {},
"image": "http://d1a3f4spazzrp4.cloudfront.net/car.jpg",
"display_name": "POOL",
"product_id": "26546650-e557-4a7b-86e7-6a3942445247",
"shared": true,
},
{
"capacity": 4,
"description": "The low-cost Uber",
"price_details": {
"distance_unit": "mile",
"cost_per_minute": 0.26,
"service_fees": [
{
"fee": 1.0,
"name": "Safe Rides Fee"
}
],
"minimum": 5.0,
"cost_per_distance": 1.3,
"base": 2.2,
"cancellation_fee": 5.0,
"currency_code": "USD"
},
"cash_enabled": false,
"image": "http://d1a3f4spazzrp4.cloudfront.net/car.jpg",
"display_name": "uberX",
"product_id": "a1111c8c-c720-46c3-8534-2fcdd730040d",
"shared": false
}
]
}


My entity class looks like:

public class Products {

@JsonProperty
private int capacity;
@JsonProperty
private String description;
@JsonProperty
private String price_details;
@JsonProperty
private String image;
@JsonProperty
private Boolean cash_enabled;
@JsonProperty
private Boolean shared;
@JsonProperty
private String short_description;
@JsonProperty
private String display_name;

//getter, setter methods
}


and wrapper class looks like:

public class ProductsModel{
private List <Products> product;

public List<Products> getProduct() {
return product;
}
public void setProduct(List<Products> product) {
this.product = product;
}
}


now when i try map the JSON to my wapper class and try to print the products .. it is giving "null" not sure why .. please help to point out why.

private ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
productsModel = mapper.readValue(line1, ProductsModel.class);
response.getWriter().println("get products:" + productsModel.getProduct()); //<-- returns null - not sure why

Answer

Use the below Product and ProductsModel class it will work fine.

Product entiry:

public class Products {

    @JsonProperty
    private int capacity;

    @JsonProperty
    private String description;

    @JsonProperty
    private Map price_details;

    @JsonProperty
    private String image;

    @JsonProperty
    private Boolean cash_enabled;

    @JsonProperty
    private Boolean shared;

    @JsonProperty
    private String short_description;

    @JsonProperty
    private String display_name;
}

ProductsModel wrapper class:

public class ProductsModel {

    private List<Products> products;

    public List<Products> getProducts() {
        return products;
    }

    public void setProducts(List<Products> products) {
        this.products = products;
    }
}

In your code there were two mistakes:

  1. The price_details value in json is a Object not string hence it should be a Map or some PriceDetails entity class which contain properties available inside the price_details json. In my reply i just used Map.
  2. The name of key in ProductsModel wrapper class was product it should be products.

I just made both the changes in Product and ProductsModel class and it worked fine.