Pravat Panda
Pravat Panda

Reputation: 1090

Javascript map from java map

I want to create a javascript map from a java map to set a dropdown value depending upon the value selected in another dropdown. Below is the code(not working):

var categoryAndReportsMap = new Object();
<% 
Map<String,Set<String>> categoryAndReportsJ = (Map<String,Set<String>>)      request.getAttribute("categoryAndReports");
for(Map.Entry<String,Set<String>> e : categoryAndReportsJ.entrySet()){ %>
categoryAndReportsMap[ <% e.getKey(); %> ] = <% e.getValue(); %>;
<% } %>

Please suggest how can I achieve this.

Upvotes: 2

Views: 8106

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

You need quotes around the keys and values :

categoryAndReportsMap["<%= e.getKey() %>"] = "<%= e.getValue() %>";

But this supposes those strings don't contain quotes themselves. The best solution would be to use a JSON serializer like the excellent gson, this would be as simple as

var categoryAndReportsMap = <%= gson.toJson(categoryAndReportsJ) %>;

Upvotes: 3

Related Questions