0

I have made a

HashMap<PriceBreak, ArrayList<PricingElement>>

in my Java code and pushed it to the client through GSON. Now in JavaScript, if I output the Object to the console it looks like this:

Object {
         PriceBreak [amount=50-99, discountedPrice=n/a] orderNo:0 forDeletion: false forChanging: false forAdding: false: Array[5],
         PriceBreak [amount=250+, discountedPrice=n/a] orderNo:0 forDeletion: false forChanging: false forAdding: false: Array[5], 
         PriceBreak [amount=1-9, discountedPrice=n/a] orderNo:0 forDeletion: false forChanging: false forAdding: false: Array[5], 
         PriceBreak [amount=100-249, discountedPrice=n/a] orderNo:0 forDeletion: false forChanging: false forAdding: false: Array[5], …
}

And I have no idea how to access the key(PriceBreak) members or values (ArrayList). Looks like each key is named the same (PriceBreak) and I don't know how to access a specific one. Any advice?

Gangnus
  • 24,044
  • 16
  • 90
  • 149
kacpr
  • 440
  • 1
  • 8
  • 28
  • It looks like your data isn't generated properly. It looks like the whole `PriceBreak [amount=50-99, discountedPrice=n/a] orderNo:0 forDeletion: false forChanging: false forAdding: false` part is actually the **property name** and an array is the value (that part is OK). Note that you can only use **strings** and **numbers** as property names in JavaScript, so you have to fix your Java code to serialize each `PriceBreak` instance to something more meaningful. – Felix Kling Jan 28 '14 at 08:42
  • 1
    Or is this really what you want? You could access one of the properties with `obj["PriceBreak [amount=50-99, discountedPrice=n/a] orderNo:0 forDeletion: false forChanging: false forAdding: false"]` then or [iterate over the properties](http://stackoverflow.com/q/85992/218196). Also have a look at [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Jan 28 '14 at 08:44
  • @Felix Kling Yeah, I think I failed in my logic here. Gson did exactly what I asked - made key/value pairs. Though it's not particularity useful in my case when I have to access the values using a long parameter like that. I think an array of some new class containing PriceBreak and an ArrayList would suit me better. edit: but I'll check iterating over property first – kacpr Jan 28 '14 at 08:50
  • Yeah, changing the structure would be better. Those are really strange property names and rather confusing in the long run. Plus you cannot access any information of those `PriceBreak` objects, because they are just strings in JS. – Felix Kling Jan 28 '14 at 08:56

1 Answers1

0

Java

HashMap<PriceBreak, ArrayList<PricingElement>> m;
...
Set<PriceBreak> s=m.keySet(); 
Collection<ArrayList<PricingElement>> c=m.values();

javascript:

// get keys
var keys = [];
for(var k in m) keys.push(k.key);

//get a value:
v=m[someKey];
Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • Isn't that Java code? I'm asking how to access the Object in JavaScript after it was stringified and sent. Sorry if that wasn't clear in my question. – kacpr Jan 28 '14 at 08:30
  • You have put "java" tag here...? – Gangnus Jan 28 '14 at 08:31
  • Well, it is a Java's HashMap that was converted so I thought it would be relevant. – kacpr Jan 28 '14 at 08:32
  • Ok, let's leave it as a java solution and you'll wait for a javascript one. – Gangnus Jan 28 '14 at 08:32
  • I think you should say, that you have created the message by Java, but reading it by Javascript. And you want for a javascript solution. Then tags are correct. Simply removing the tag won't help - people will see the Java types anyway. – Gangnus Jan 28 '14 at 08:34
  • 1
    I did it, too, hope, the answer is more useful :-) – Gangnus Jan 28 '14 at 08:40