1

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.

Lenny Carmi
  • 763
  • 1
  • 6
  • 12
  • This statement: `"How can I set this on my product object in JAVA."` isn't clear. Please try to re-phrase and clarify. If you're trying to add a collection of Products to the Product class, don't. It doesn't belong there. – Hovercraft Full Of Eels Oct 11 '15 at 09:51
  • Basically I am trying to add the size to product class. – Lenny Carmi Oct 11 '15 at 09:53
  • Again, clarify. Is size a collection of Product objects? Again if it is, the correct answer is that you should not be trying to add this to Product. Yours sounds like an XY Problem where you're looking for a specific solution to an incorrect question. Better to tell us more of the overall problem you're trying to solve, not how you're trying to solve it. – Hovercraft Full Of Eels Oct 11 '15 at 09:55
  • I have added a bit more information. Tell me if the question is now clear. – Lenny Carmi Oct 11 '15 at 10:00

2 Answers2

1

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;
Zack
  • 1,527
  • 2
  • 20
  • 32
0

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.

Community
  • 1
  • 1
Robert Reiner
  • 531
  • 3
  • 9