How to get Json into a JS variable out of a Scala

2019-06-12 11:49发布

I'm using Play framework and trying to send a Json string from the server as a parameter to a Scala template.

@(aj: String)
@import play.api.libs.json.Json
<!DOCTYPE html>
<html lang="en">
   <body>
      <div id="container">
        @aj
      </div>

    <script>
       var ab=@Json.toJson(aj);
       var a = JSON.parse(ab);
    </script>
    </body>
 </html>

I get the Json String displayed inside the container div. But I'm not able to get the Json out of 'aj' into a javascript variable. Please help.

1条回答
倾城 Initia
2楼-- · 2019-06-12 12:04

In view you can do:

<script>
var json = @Html(aj),
obj = JSON.parse(json);
</script>
查看更多
登录 后发表回答