杰克逊与JSON:接收" null"对象

时间:2016-09-04 03:25:21

标签: java json jackson uber-api

我需要将Uber Products JSON响应从here转换为Java对象。我正在使用Jackson进行JSON处理。我的输入JSON看起来像:

    {
      "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
        }
      ]
    }

我的实体类看起来像:

    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
    } 

和包装类看起来像:

    public class ProductsModel{
        private List <Products> product;

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

现在当我尝试将JSON映射到我的wapper类并尝试打印产品时......它正在给出&#34; null&#34;不确定为什么..请帮助指出原因。

    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

1 个答案:

答案 0 :(得分:0)

使用下面的Product和ProductsModel类,它可以正常工作。

产品情况:

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包装类:

public class ProductsModel {

    private List<Products> products;

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

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

在你的代码中有两个错误:

  1. json中的 price_details 值是一个Object not string因此它应该是 Map 或一些 PriceDetails 实体类,其中包含可用的属性 price_details json。在我的回复中,我刚使用了Map。
  2. ProductsModel 包装类中的键名是产品,它应该是产品
  3. 我刚刚在Product和ProductsModel类中进行了更改,并且工作正常。