简体   繁体   中英

How can I assign multiple lists to a property of my object?

I have a product object that has a number of properties like this

public class Product {
     private int deal_id;
     private String deal_key;
     private String deal_title;
     private String url_title;
     private int deal_value;
.........//setters and getters
}

Every product has a number of sizes that can also be 0. I want to assign sizes to my product from values in the json data I receive(below). I am extending this class and now I need to add another property of size which is an array of arrays. I need to set it from json on the api that like this.

"size" : [ { "product_size_id": "695", "deal_id": "179", "size_name": "None", "quantity": "1", "size_id": "1" }, 
           { "product_size_id": "695", "deal_id": "179", "size_name": "None", "quantity": "1", "size_id": "1" } ]

How can I set this on my product object in JAVA. I have tried looking up Hashmaps and Arraylist but havent found a way to do it correctly.

You can create an object for Size with all your properties

public class Size {
    private int product_size_id;
    private int deal_id;
    private String size_name;
    private int quantity;
    private int size_id;
....setters and getters
}

Then you can set size as object and add it to Product as a List

private ArrayList<Size> yoursizes;

Two things that may help to find a solution to your problem.

First you may want to parse the JSON input. This answer may help you to get this done.

Second I think you need a data structure that allows to assign multiple lists to a value (or something similar). There are libraries for Java to help you do this. Guava or Commons Collections are two examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM