简体   繁体   English

将JSON数据引导到Scala PlayFramework模板中

[英]Bootstrapping JSON data into Scala PlayFramework templates

Goal is to go from a Model/ViewModel written in Scala to raw JSON that can be bootstrapped into the view template so as to avoid making requests for the JSON data after the page load. 目标是从用Scala编写的Model / ViewModel转换为可以引导到视图模板中的原始JSON,以避免在页面加载后发出对JSON数据的请求。

And example of what I've been playing around with but I haven't had much luck: 我一直在玩的例子,但我运气不好:

@(todos: play.api.libs.json.JsValue)
@import play.api.libs.json.Json

<html>
   <head>...</head>
   <body>...</body>

   <script>
      var todos = JSON.parse(' @Json.stringify(todos) ');
   </script>
</html>

Basically its spitting out a lot of quoted text to the effect of: 基本上它吐出了很多引用的文字,效果如下:

[{&quot;id&quot;:&quot;:&quot;294858e2-c9eb-4f50-9eac-47b257573d83&quot;}] 

Haven't had much luck with Google or the PlayFramework docs so I'd love some help. 没有太多运气与谷歌或PlayFramework文档,所以我喜欢一些帮助。

The Play template engine will escape any strings you render to HTML, which will thoroughly mangle your JSON. Play模板引擎将转义您呈现给HTML的任何字符串,这将彻底破坏您的JSON。

To output it verbatim, do @Html(Json.stringify(todos)) , as described here . 为了输出逐字,做@Html(Json.stringify(todos))如所描述这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM