如何将Scala Map [String,Map [String,String]]传递给Js对象

时间:2016-04-19 06:12:31

标签: javascript scala playframework

我想在Play框架视图中将Scala Map值传递给JavaScript JSON onject

我得到“SyntaxError:JSON.parse:JSON数据的第1行第10列的意外字符”当我检查以下代码时

val mapTobePassToJs = Map(

      "cg1" -> Map(
        "path" -> "/var/logs/cdr1/cdr",
        "name"  -> "CG-1"

      ),

      "cg2" -> Map(
        "path" -> "/var/logs/cdr2/cdr",
        "name"  -> "CG-2"
      ),

      "cg3" -> Map(
        "path" -> "/var/logs/cdr3/cdr",
        "name"  -> "CG-3"
      ),

      "cg4" -> Map(
        "path" -> "/var/logs/cdr3/cdr",
        "name"  -> "CG-4"
      )
    )

<script type="text/javascript">

        var aChartList = jQuery.parseJSON("@JSONObject(mapTobePassToJs).toString()".replace(/&quot;/g,'"'));

</script>

任何人都请帮助我

1 个答案:

答案 0 :(得分:3)

因为你想要Json,你为什么不使用val mapTobePassToJs = Json.obj( "cg1" -> Json.obj( "path" -> "/var/logs/cdr1/cdr", "name" -> "CG-1" ), "cg2" -> Json.obj( "path" -> "/var/logs/cdr2/cdr", "name" -> "CG-2" ), "cg3" -> Json.obj( "path" -> "/var/logs/cdr3/cdr", "name" -> "CG-3" ), "cg4" -> Json.obj( "path" -> "/var/logs/cdr3/cdr", "name" -> "CG-4" ) ) 。以下是你的结构。

var resp = request.GetResponse();