最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Scala Play - How to convert a list of Scala Strings into an Array of javascript Strings (avoiding the " issue)? - S

programmeradmin3浏览0评论

When converting a Scala list of Strings into a javascript Array of Strings with the Play template engine, you probably start with something like this ...

var strArray = [@scalaListOfStrings.mkString(",")];

... and will find out that this is not working, because the quotes around the strings are missing. Next you might try something like this ...

var strArray = [@scalaListOfStrings.map(s => "\"" + s + "\"").mkString(",")];

... only to find out that this will wrap the strings in " and not ". The only way I was able to make this work was with ...

var strArray = [@Html(scalaListOfStrings.map(s => "\"" + s + "\"").mkString(","))];

... and my question is: Is this the best/only way to do this?

When converting a Scala list of Strings into a javascript Array of Strings with the Play template engine, you probably start with something like this ...

var strArray = [@scalaListOfStrings.mkString(",")];

... and will find out that this is not working, because the quotes around the strings are missing. Next you might try something like this ...

var strArray = [@scalaListOfStrings.map(s => "\"" + s + "\"").mkString(",")];

... only to find out that this will wrap the strings in " and not ". The only way I was able to make this work was with ...

var strArray = [@Html(scalaListOfStrings.map(s => "\"" + s + "\"").mkString(","))];

... and my question is: Is this the best/only way to do this?

Share Improve this question asked May 12, 2013 at 6:20 Roland TritschRoland Tritsch 813 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can rely on the Json.toJson() method to make the conversion

@import play.api.libs.json._

var strArray = @Json.stringify(Json.toJson(List("hello", "world", "everybody")))

Don't forget @Html.

@Html(Json.stringtify(Json.toJson(Scala object)))

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论