title: Java Request Attributes to JavaScript Objects
body: <p>I have a jQuery library that wants you to pass it a JavaScript object as an option. For example, this works:</p>
  
  <pre><code>//Hardcoded JavaScript object
  var myData = { "A" : "1", "B" : "2", "C" : "3" };
  
  //Call to jQuery with object as an option
  $("#mySelectorId").myJqueryFunction({values: myData});
  </code></pre>
  
  <p>I need the <code>myData</code> object to not be hardwired like that. </p>
  
  <p>I can easy get the "A to 1, B to 2, C to 3" structure in my Java code, put it in a <code>Map</code> (or some other object), and add it to the request via <code>request.setAttribute("myData", myMap);</code>. I am just not sure how to convert the <code>Map</code> (etc) into the needed JavaScript object. Here is what I have tried.</p>
  
  <pre><code>//Java
  Map&lt;String, String&gt; myMap = new TreeMap&lt;String, String&gt;();
  map.put("A", "1");
  map.put("B", "2");
  map.put("C", "3");
  request.setAttribute("myMap", myMap);
  
  //JSP
  &lt;c:set var="myData" value="{" /&gt;
  &lt;c:forEach var="mapEntry" items="${myMap}" varStatus="counter"&gt;
      &lt;c:set var="myData" value="${myData}'${mapEntry.key}' : '${mapEntry.value}'" /&gt;
      &lt;c:if test="${counter.count &lt; fn:length(myMap)}"&gt;
          &lt;c:set var="myData" value="${myData}, " /&gt;
      &lt;/c:if&gt;
  &lt;/c:forEach&gt;
  &lt;c:set var="myData" value="${myData}}" /&gt;
  
  &lt;script&gt;
      var myData = '${myData}';
      $("#mySelectorId").myJqueryFunction({values: myData});
  &lt;/script&gt;
  </code></pre>
  
  <p>This works until the <code>myJqueryFunction</code> reaches a point where it tries to do something like this:</p>
  
  <pre><code>var i = values[code];
  </code></pre>
  
  <p>Essentially, it is trying to reference a particular variable on the object (e.g. trying to get the value of "B", which is 2). Note that <code>code</code> is a function parameter that holds the A/B/C value.</p>
  
  <p>I have tried messing around with including or not including the quotes around <code>${mapEntry.key}</code> and things like that, but nothing seems to work. </p>
  
  <p>Does anyone know how to get this working?</p>