将json嵌套到Java对象List <object>返回期望值,但是List <definedobject>返回null值

时间:2019-04-22 17:44:10

标签: java arrays json list

我有以下json,我想将其存储到java对象中:

{  
       "shippingDetails":{  
          "address":"khalda",
          "country":"Jordan",
          "town":"amman"
       },
       "product":[  
          {  
             "product":{  
                "id":2,
                "name":"Wrap Dress",
                "price":330,
                "salePrice":165,
                "discount":50,
                "pictures":[  
                   "assets/images/fashion/product/1.jpg",
                   "assets/images/fashion/product/1.jpg",
                   "assets/images/fashion/product/1.jpg",
                   "assets/images/fashion/product/1.jpg"
                ],
                "shortDetails":"test",
                "description":"test",
                "stock":2,
                "new":true,
                "sale":false,
                "category":"women",
                "colors":[  
                   "gray",
                   "orange"
                ],
                "size":[  
                   "M",
                   "L",
                   "XL"
                ],
                "tags":[  
                   "caprese",
                   "puma",
                   "lifestyle"
                ],
                "variants":[  
                   {  
                      "color":"gray",
                      "images":"assets/images/fashion/product/1.jpg"
                   },
                   {  
                      "color":"orange",
                      "images":"assets/images/fashion/product/1.jpg"
                   }
                ]
             },
             "quantity":1
          }
       ],
       "totalAmount":330
    }

我有以下Java Bean: 主要对象类称为Order,它具有

@Data
@Entity
@Table(name = "orders")
public class Order {
    @Id
    @Column(name="shippingDetailsId")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @OneToOne(cascade = {CascadeType.ALL})
    private ShippingDetails shippingDetails;
    //@OneToMany(cascade = {CascadeType.ALL})
    //List<Product> products;
    List<Object> product = new ArrayList<>();
    private float totalAmount;
}

和内部对象类是:

@Data
@Entity
@Table(name = "products")
public class Product {
    @Id
    @Column(name = "productId")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @ManyToOne
    private Order order;
    private Boolean isNew;
    private String salePrice;
    private String shortDetails;
    private String discount;
    private String description;
    private Variants[] variants;
    private String[] pictures;
    private String[] colors;
    private String[] tags;
    private String sale;
    private String[] size;
    private String price;
    private String name;
    private String stock;
    private String category;
}

和运输详细信息bean:

@Data
@Entity
@Table(name = "shippingDetails")
public class ShippingDetails {
    @Id
    @Column(name="shippingDetailsId")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String address;
    private String country;
    private String town;
}

我尝试了很多方法,但是没有一种方法有效,首先,我试图在控制器中接受一个Order对象,然后尝试将其存储但不起作用,结果是:

当使用“列出”时,所有值都将按需要打印到控制台并插入数据库中,但在同一表中“订单”不是我想要的,我希望将“产品”保存在称为“产品”的单独表中所以我用了列表。

当使用List时,产品的值为空。

然后,我尝试从控制器中接受一个字符串,并使用GSON库将其转换为Order对象,但效果不佳,我遇到了很多异常,试图弄清楚它并尝试了许多方法是在stackoverflow和其他网站上提到的,但是没有运气,请不要因为我已经验证了json字符串,并且它是有效的json。

2 个答案:

答案 0 :(得分:1)

如果您看到JSON中的层次结构,则该层次结构与Java对象不匹配。

 "product":[  // Parent-Product
          {  
             "product":{  // Child-Product
                "id":2,

直接映射到Java就像

List<Map<String,Product>> products

如果您可以选择更改JSON文件,请更改为以下内容并摆脱“父子产品”层次结构。

"product":[
  {
    "id":..
  },
  {
    "id":..
  }
  ..
]

答案 1 :(得分:0)

您可以在此处使用转换器。它从json生成Java对象。 http://www.jsonschema2pojo.org/(包括gson和jackson在内的其他支持者)

这将确保您具有Json到Java的正确映射,反之亦然。