title: How do I pass a JSONObject to a Play Framework for Java template's javascript?
body: <p>I am working with the Play framework (2.4) for Java.  I want to pass a JSONObject to a javascript used inside one of the Play view templates.<br>
  On the Java side I prepare the JSONObject, like so:</p>
  
  <p>(Keep in mind that this is a test vehicle.)</p>
  
  <pre><code>    public static Result showBusinesses(){
      List&lt;Item&gt; list = new ArrayList&lt;Item&gt;();
      Item r = new Item();
      r.id = "23234";
      r.name = "Joes hardware";
      Item s = new Item();
      s.id = "23254";
      s.name = "Martys collision";
      list.add(r);
      list.add(s);
      return ok(views.html.wheel.render(getJSONObject(list)));
  }
  
  public static JSONObject getJSONObject(List&lt;Item&gt; list){
      JSONObject jsonObject = new JSONObject();
      try{
          for (int i = 0; i &lt; list.size(); i++) {
          jsonObject.put(list.get(i).id, list.get(i).name);
          }
      }catch (JSONException e) {
  
      }
      return jsonObject;
  }
  </code></pre>
  
  <p>In my Play template, I accept the JSONObject parameter:</p>
  
  <pre><code>@(item : org.json.JSONObject)
  
  @import helper._
  @import helper.twitterBootstrap._
  @import play.api.libs.json.Json 
  </code></pre>
  
  <p>...</p>
  
  <p>So far, so good.   </p>
  
  <p>Until I attempt to use the object in my javascript:</p>
  
  <p>If I place my object, @item, anywhere in the template besides inside the javascript, I get this:
  {"23254":"Martys Pancakes","23234":"Joes place"}; 
  which looks like a properly formed var to me.</p>
  
  <p>I am inserting the JSONObject into the javascript like this:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
   businesses = @item;
  </code></pre>
  
  <p>and I expect that to translate like this:</p>
  
  <pre><code>businesses = {
      "23332"  : "Joe's hardware",
      "56755"  : "Marty's collision"
  };
  </code></pre>
  
  <p>And yet the object does not behave as expected.  I suspect that I am not passing the parameter to the javascript in the correct way.</p>
  
  <p>Can anyone enlighten me?  Thanks.</p>