0

I am returning HashMap<Long,String> from server on one of the Ajax calls. How can I iterate over the it to extract key,value pair?

Thank you

3
  • 3
    What does the response look like on the client? Commented Sep 17, 2013 at 14:23
  • Can you post the returned (I guess) JSON ? Commented Sep 17, 2013 at 14:23
  • Convert the Map to JSON with Jackson: stackoverflow.com/questions/11136376/… Commented Sep 17, 2013 at 14:25

1 Answer 1

1

Return it as JSON object:

{1:"String",5:"Foo"}

On the client, you can then access the elements then using data[1] and data[5]. While it looks like an array access, JavaScript will do the right thing (Kudos to Felix Kling for pointing that out).

Sign up to request clarification or add additional context in comments.

3 Comments

"Note that you can't use numbers as JavaScript object keys" That's incorrect. Example: var obj = {5: 'foo'};. It's true that the property has to be accessed with obj[5], but that's not "array access", it's called bracket notation and is just an alternative way to access properties. See the MDN documentation. In fact, arrays are just objects themselves and each element of the array is a property (with a numeric property name) of that underlying object.
@FelixKling: Thanks, fixed. I should really write more code :-)
Thank you guys it is returned as object hash(associative array) and I was able to retrieve values through simple for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.