1

In java i have hashmap

Map<String , List<Employee>> employeeMap = new HashMap<String , List<Employee>> ();
employeeMap.put(1, new Employee());
........

I am adding it in request attribute

Now in javascript i need to loop over over map, to get keys and values

Here what i tried

Approach 1 :-

var empMap = '${employeeMap}'; 
//here is the example value of map in javascript which i see while debugging
//var empMap = {emp1_100=[com.Employee@5b7b4bc5], emp2...

for (var key in empMap) {
    alert(key + ': ' + empMap[key]);
}

But getting syntax error

 SyntaxError: missing : after property id

Approach 2 :- with jquery

 var jsonMap  = $.stringify(empMap);

 $.each( jsonMap, function( key, value ) {
  alert( key + ": " + value );
});

But its not working either. It print some characters.

I am not sure what i am missing here ?

1

1 Answer 1

1

Looks like you are simply writing whatever the .toString() method of your Map returns to the request, which is not a good idea in the first place.

If I were you, I would convert your Map into a JSON object (there various ways to do that, depending on your system) and then append the String representation of this JSON object to the request, because then you can simply call "JSON.parse( ... );" in JavaScript to transform it into a JavaScript object without having to do any parsing yourself.

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

Comments

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.