Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Twirl模板中将Scala JsValue转换为JavaScript对象_Javascript_Json_Scala_Playframework - Fatal编程技术网

在Twirl模板中将Scala JsValue转换为JavaScript对象

在Twirl模板中将Scala JsValue转换为JavaScript对象,javascript,json,scala,playframework,Javascript,Json,Scala,Playframework,我的问题非常简单。我有一个play.api.libs.json.JsValueScala对象,它是通过Linkedin的RESTAPI获得的 看起来是这样的: {"apiStandardProfileRequest":{"headers":{"_total":1,"values":[{"name":"x-li-auth-token","value":"name:ry53"}]},"url":"https://api.linkedin.com/v1/people/1WO4xzvPJ1"},"cur

我的问题非常简单。我有一个
play.api.libs.json.JsValue
Scala对象,它是通过Linkedin的RESTAPI获得的

看起来是这样的:

{"apiStandardProfileRequest":{"headers":{"_total":1,"values":[{"name":"x-li-auth-token","value":"name:ry53"}]},"url":"https://api.linkedin.com/v1/people/1WO4xzvPJ1"},"currentShare":{"author":{"firstName":"Christophe","id":"1WO4xzvPJ1","lastName":"Bram"},"comment":"Cruited is searching for a second web developer!","content":{"description":"Cruited is a self-financed startup. We are a team of six, with four nationalities, working from three countries. Two of us share a desk on the fourth floor of a building just in front of the royal castle in Stockholm. One works from home, we’re not..","eyebrowUrl":"https://cruited.com/jobba-med-oss/frontend-developer","resolvedUrl":"https://cruited.com/jobba-med-oss/frontend-developer/","shortenedUrl":"https://lnkd.in/dNk-qZx","submittedImageUrl":"https://cruited.com/app/uploads/2015/09/Slottet_1920.jpg","submittedUrl":"https://cruited.com/jobba-med-oss/frontend-developer","thumbnailUrl":"https://media.licdn.com/media-proxy/ext?w=80&h=100&f=&hash=tEfv4M6YemBquw5%2BAdNMhg355VE%3D&ora=1%2CaFBCTXdkRmpGL2lvQUFBPQ%2CxAVta5g-0R6pgQ4T1hklrq2OoACx9FcIUJLQDWH1GDqo-tSFf3fgZqSDJvbl8Rl5eSUDkBpoP7g","title":"Frontend Web Developer - Cruited"},"id":"s6065129416948744192","source":{"serviceProvider":{"name":"LINKEDIN"}},"timestamp":1446039537000,"visibility":{"code":"anyone"}},"emailAddress":"cbramdit@gmail.com","firstName":"Christophe","formattedName":"Christophe Bram","headline":"Software Architect, Product Designer at Cruited; freelance web developer on the side","id":"1WO4xzvPJ1","industry":"Computer Software","lastName":"Bram","location":{"country":{"code":"se"},"name":"Stockholm, Sweden"},"numConnections":122,"numConnectionsCapped":false,"pictureUrl":"https://media.licdn.com/mpr/mprx/0_oivHPf2fD-dkMRr1HC1BPuE83POoUYr1eGTBPuIrOnoRkVFPQL5Xj2xOfjYq4MA0IhB9yIzGufhD","pictureUrls":{"_total":1,"values":["https://media.licdn.com/mpr/mprx/0_GlrRkpuP6VS9Pa-c8AhzCgylhyANcx-c8tmnCjGxSHKBPY8MGTlchRwpahf"]},"positions":{"_total":2,"values":[{"company":{"id":3185136,"industry":"Human Resources","name":"Cruited","size":"1-10 employees","type":"Privately Held"},"id":658668057,"isCurrent":true,"startDate":{"month":1,"year":2015},"title":"Partner, Software Architect and Product Designer"},{"company":{"name":"8b"},"id":583856605,"isCurrent":true,"startDate":{"month":2,"year":2014},"summary":"Currently building a career platform for Cruited.\n\nRefined the Citizen Hive project requirements into specifications and UI mockups.\n\nFully managed, specified and implemented the Howl at Them web application used by Wolf PAC.","title":"Freelance Web Developer"}]},"publicProfileUrl":"https://www.linkedin.com/pub/christophe-bram/42/64a/26a","siteStandardProfileRequest":{"url":"https://www.linkedin.com/profile/view?id=AAoAAAj-cPIBk82OHE6XPJpm_QgKwVSl5hHf3KQ&authType=name&authToken=ry53&trk=api*a296796*s304357*"}}
<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
        CR.MyController(
            @Html(linkedinProfile.toString)
        );
    });
</script>
<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
        CR.MyController(
        {"apiStandardProfileRequest":{"headers":{"_total":1,"values":[{"name":"x-li-auth-token","value":"name:ry53"}]},"url":"https://api.linkedin.com/v1/people/1WO4xzvPJ1"},"currentShare":{"author":{"firstName":"Christophe","id":"1WO4xzvPJ1","lastName":"Bram"},"comment":"Cruited is searching for a second web developer!","content":{"description":"Cruited is a self-financed startup. We are a team of six, with four nationalities, working from three countries. Two of us share a desk on the fourth floor of a building just in front of the royal castle in Stockholm. One works from home, we’re not..","eyebrowUrl":"https://cruited.com/jobba-med-oss/frontend-developer","resolvedUrl":"https://cruited.com/jobba-med-oss/frontend-developer/","shortenedUrl":"https://lnkd.in/dNk-qZx","submittedImageUrl":"https://cruited.com/app/uploads/2015/09/Slottet_1920.jpg","submittedUrl":"https://cruited.com/jobba-med-oss/frontend-developer","thumbnailUrl":"https://media.licdn.com/media-proxy/ext?w=80&h=100&f=&hash=tEfv4M6YemBquw5%2BAdNMhg355VE%3D&ora=1%2CaFBCTXdkRmpGL2lvQUFBPQ%2CxAVta5g-0R6pgQ4T1hklrq2OoACx9FcIUJLQDWH1GDqo-tSFf3fgZqSDJvbl8Rl5eSUDkBpoP7g","title":"Frontend Web Developer - Cruited"},"id":"s6065129416948744192","source":{"serviceProvider":{"name":"LINKEDIN"}},"timestamp":1446039537000,"visibility":{"code":"anyone"}},"emailAddress":"cbramdit@gmail.com","firstName":"Christophe","formattedName":"Christophe Bram","headline":"Software Architect, Product Designer at Cruited; freelance web developer on the side","id":"1WO4xzvPJ1","industry":"Computer Software","lastName":"Bram","location":{"country":{"code":"se"},"name":"Stockholm, Sweden"},"numConnections":122,"numConnectionsCapped":false,"pictureUrl":"https://media.licdn.com/mpr/mprx/0_oivHPf2fD-dkMRr1HC1BPuE83POoUYr1eGTBPuIrOnoRkVFPQL5Xj2xOfjYq4MA0IhB9yIzGufhD","pictureUrls":{"_total":1,"values":["https://media.licdn.com/mpr/mprx/0_GlrRkpuP6VS9Pa-c8AhzCgylhyANcx-c8tmnCjGxSHKBPY8MGTlchRwpahf"]},"positions":{"_total":2,"values":[{"company":{"id":3185136,"industry":"Human Resources","name":"Cruited","size":"1-10 employees","type":"Privately Held"},"id":658668057,"isCurrent":true,"startDate":{"month":1,"year":2015},"title":"Partner, Software Architect and Product Designer"},{"company":{"name":"8b"},"id":583856605,"isCurrent":true,"startDate":{"month":2,"year":2014},"summary":"Currently building a career platform for Cruited.\n\nRefined the Citizen Hive project requirements into specifications and UI mockups.\n\nFully managed, specified and implemented the Howl at Them web application used by Wolf PAC.","title":"Freelance Web Developer"}]},"publicProfileUrl":"https://www.linkedin.com/pub/christophe-bram/42/64a/26a","siteStandardProfileRequest":{"url":"https://www.linkedin.com/profile/view?id=AAoAAAj-cPIBk82OHE6XPJpm_QgKwVSl5hHf3KQ&authType=name&authToken=ry53&trk=api*a296796*s304357*"}}
        );
    });
</script>
我的Twirl模板中提供了
linkedinProfile:play.api.libs.json.JsValue
对象

但我无法将其作为JavaScript对象传递给JavaScript控制器。我试着这样做:

{"apiStandardProfileRequest":{"headers":{"_total":1,"values":[{"name":"x-li-auth-token","value":"name:ry53"}]},"url":"https://api.linkedin.com/v1/people/1WO4xzvPJ1"},"currentShare":{"author":{"firstName":"Christophe","id":"1WO4xzvPJ1","lastName":"Bram"},"comment":"Cruited is searching for a second web developer!","content":{"description":"Cruited is a self-financed startup. We are a team of six, with four nationalities, working from three countries. Two of us share a desk on the fourth floor of a building just in front of the royal castle in Stockholm. One works from home, we’re not..","eyebrowUrl":"https://cruited.com/jobba-med-oss/frontend-developer","resolvedUrl":"https://cruited.com/jobba-med-oss/frontend-developer/","shortenedUrl":"https://lnkd.in/dNk-qZx","submittedImageUrl":"https://cruited.com/app/uploads/2015/09/Slottet_1920.jpg","submittedUrl":"https://cruited.com/jobba-med-oss/frontend-developer","thumbnailUrl":"https://media.licdn.com/media-proxy/ext?w=80&h=100&f=&hash=tEfv4M6YemBquw5%2BAdNMhg355VE%3D&ora=1%2CaFBCTXdkRmpGL2lvQUFBPQ%2CxAVta5g-0R6pgQ4T1hklrq2OoACx9FcIUJLQDWH1GDqo-tSFf3fgZqSDJvbl8Rl5eSUDkBpoP7g","title":"Frontend Web Developer - Cruited"},"id":"s6065129416948744192","source":{"serviceProvider":{"name":"LINKEDIN"}},"timestamp":1446039537000,"visibility":{"code":"anyone"}},"emailAddress":"cbramdit@gmail.com","firstName":"Christophe","formattedName":"Christophe Bram","headline":"Software Architect, Product Designer at Cruited; freelance web developer on the side","id":"1WO4xzvPJ1","industry":"Computer Software","lastName":"Bram","location":{"country":{"code":"se"},"name":"Stockholm, Sweden"},"numConnections":122,"numConnectionsCapped":false,"pictureUrl":"https://media.licdn.com/mpr/mprx/0_oivHPf2fD-dkMRr1HC1BPuE83POoUYr1eGTBPuIrOnoRkVFPQL5Xj2xOfjYq4MA0IhB9yIzGufhD","pictureUrls":{"_total":1,"values":["https://media.licdn.com/mpr/mprx/0_GlrRkpuP6VS9Pa-c8AhzCgylhyANcx-c8tmnCjGxSHKBPY8MGTlchRwpahf"]},"positions":{"_total":2,"values":[{"company":{"id":3185136,"industry":"Human Resources","name":"Cruited","size":"1-10 employees","type":"Privately Held"},"id":658668057,"isCurrent":true,"startDate":{"month":1,"year":2015},"title":"Partner, Software Architect and Product Designer"},{"company":{"name":"8b"},"id":583856605,"isCurrent":true,"startDate":{"month":2,"year":2014},"summary":"Currently building a career platform for Cruited.\n\nRefined the Citizen Hive project requirements into specifications and UI mockups.\n\nFully managed, specified and implemented the Howl at Them web application used by Wolf PAC.","title":"Freelance Web Developer"}]},"publicProfileUrl":"https://www.linkedin.com/pub/christophe-bram/42/64a/26a","siteStandardProfileRequest":{"url":"https://www.linkedin.com/profile/view?id=AAoAAAj-cPIBk82OHE6XPJpm_QgKwVSl5hHf3KQ&authType=name&authToken=ry53&trk=api*a296796*s304357*"}}
<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
        CR.MyController(
            @Html(linkedinProfile.toString)
        );
    });
</script>
<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
        CR.MyController(
        {"apiStandardProfileRequest":{"headers":{"_total":1,"values":[{"name":"x-li-auth-token","value":"name:ry53"}]},"url":"https://api.linkedin.com/v1/people/1WO4xzvPJ1"},"currentShare":{"author":{"firstName":"Christophe","id":"1WO4xzvPJ1","lastName":"Bram"},"comment":"Cruited is searching for a second web developer!","content":{"description":"Cruited is a self-financed startup. We are a team of six, with four nationalities, working from three countries. Two of us share a desk on the fourth floor of a building just in front of the royal castle in Stockholm. One works from home, we’re not..","eyebrowUrl":"https://cruited.com/jobba-med-oss/frontend-developer","resolvedUrl":"https://cruited.com/jobba-med-oss/frontend-developer/","shortenedUrl":"https://lnkd.in/dNk-qZx","submittedImageUrl":"https://cruited.com/app/uploads/2015/09/Slottet_1920.jpg","submittedUrl":"https://cruited.com/jobba-med-oss/frontend-developer","thumbnailUrl":"https://media.licdn.com/media-proxy/ext?w=80&h=100&f=&hash=tEfv4M6YemBquw5%2BAdNMhg355VE%3D&ora=1%2CaFBCTXdkRmpGL2lvQUFBPQ%2CxAVta5g-0R6pgQ4T1hklrq2OoACx9FcIUJLQDWH1GDqo-tSFf3fgZqSDJvbl8Rl5eSUDkBpoP7g","title":"Frontend Web Developer - Cruited"},"id":"s6065129416948744192","source":{"serviceProvider":{"name":"LINKEDIN"}},"timestamp":1446039537000,"visibility":{"code":"anyone"}},"emailAddress":"cbramdit@gmail.com","firstName":"Christophe","formattedName":"Christophe Bram","headline":"Software Architect, Product Designer at Cruited; freelance web developer on the side","id":"1WO4xzvPJ1","industry":"Computer Software","lastName":"Bram","location":{"country":{"code":"se"},"name":"Stockholm, Sweden"},"numConnections":122,"numConnectionsCapped":false,"pictureUrl":"https://media.licdn.com/mpr/mprx/0_oivHPf2fD-dkMRr1HC1BPuE83POoUYr1eGTBPuIrOnoRkVFPQL5Xj2xOfjYq4MA0IhB9yIzGufhD","pictureUrls":{"_total":1,"values":["https://media.licdn.com/mpr/mprx/0_GlrRkpuP6VS9Pa-c8AhzCgylhyANcx-c8tmnCjGxSHKBPY8MGTlchRwpahf"]},"positions":{"_total":2,"values":[{"company":{"id":3185136,"industry":"Human Resources","name":"Cruited","size":"1-10 employees","type":"Privately Held"},"id":658668057,"isCurrent":true,"startDate":{"month":1,"year":2015},"title":"Partner, Software Architect and Product Designer"},{"company":{"name":"8b"},"id":583856605,"isCurrent":true,"startDate":{"month":2,"year":2014},"summary":"Currently building a career platform for Cruited.\n\nRefined the Citizen Hive project requirements into specifications and UI mockups.\n\nFully managed, specified and implemented the Howl at Them web application used by Wolf PAC.","title":"Freelance Web Developer"}]},"publicProfileUrl":"https://www.linkedin.com/pub/christophe-bram/42/64a/26a","siteStandardProfileRequest":{"url":"https://www.linkedin.com/profile/view?id=AAoAAAj-cPIBk82OHE6XPJpm_QgKwVSl5hHf3KQ&authType=name&authToken=ry53&trk=api*a296796*s304357*"}}
        );
    });
</script>

直接在模板中调用控制器会破坏模式

在控制器中,您通常会执行以下操作

class FooController() extends Controller {

  def index() = Action { 
    //fetch from linkedin or where ever    
    val result: JsValue = Json.obj("foo" -> "bar")
    Ok(views.html.index(result))
  }

}
视图:

@import play.api.libs.json.JsValue
@(result: JsValue)

<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
       @result
    });
</script>
@import play.api.libs.json.JsValue
@(结果:JsValue)
document.addEventListener(“DOMContentLoaded”,function()){
@结果
});
@Html(linkedinProfile.toString)
应该直接生成以下内容:

{"apiStandardProfileRequest":{"headers":{"_total":1,"values":[{"name":"x-li-auth-token","value":"name:ry53"}]},"url":"https://api.linkedin.com/v1/people/1WO4xzvPJ1"},"currentShare":{"author":{"firstName":"Christophe","id":"1WO4xzvPJ1","lastName":"Bram"},"comment":"Cruited is searching for a second web developer!","content":{"description":"Cruited is a self-financed startup. We are a team of six, with four nationalities, working from three countries. Two of us share a desk on the fourth floor of a building just in front of the royal castle in Stockholm. One works from home, we’re not..","eyebrowUrl":"https://cruited.com/jobba-med-oss/frontend-developer","resolvedUrl":"https://cruited.com/jobba-med-oss/frontend-developer/","shortenedUrl":"https://lnkd.in/dNk-qZx","submittedImageUrl":"https://cruited.com/app/uploads/2015/09/Slottet_1920.jpg","submittedUrl":"https://cruited.com/jobba-med-oss/frontend-developer","thumbnailUrl":"https://media.licdn.com/media-proxy/ext?w=80&h=100&f=&hash=tEfv4M6YemBquw5%2BAdNMhg355VE%3D&ora=1%2CaFBCTXdkRmpGL2lvQUFBPQ%2CxAVta5g-0R6pgQ4T1hklrq2OoACx9FcIUJLQDWH1GDqo-tSFf3fgZqSDJvbl8Rl5eSUDkBpoP7g","title":"Frontend Web Developer - Cruited"},"id":"s6065129416948744192","source":{"serviceProvider":{"name":"LINKEDIN"}},"timestamp":1446039537000,"visibility":{"code":"anyone"}},"emailAddress":"cbramdit@gmail.com","firstName":"Christophe","formattedName":"Christophe Bram","headline":"Software Architect, Product Designer at Cruited; freelance web developer on the side","id":"1WO4xzvPJ1","industry":"Computer Software","lastName":"Bram","location":{"country":{"code":"se"},"name":"Stockholm, Sweden"},"numConnections":122,"numConnectionsCapped":false,"pictureUrl":"https://media.licdn.com/mpr/mprx/0_oivHPf2fD-dkMRr1HC1BPuE83POoUYr1eGTBPuIrOnoRkVFPQL5Xj2xOfjYq4MA0IhB9yIzGufhD","pictureUrls":{"_total":1,"values":["https://media.licdn.com/mpr/mprx/0_GlrRkpuP6VS9Pa-c8AhzCgylhyANcx-c8tmnCjGxSHKBPY8MGTlchRwpahf"]},"positions":{"_total":2,"values":[{"company":{"id":3185136,"industry":"Human Resources","name":"Cruited","size":"1-10 employees","type":"Privately Held"},"id":658668057,"isCurrent":true,"startDate":{"month":1,"year":2015},"title":"Partner, Software Architect and Product Designer"},{"company":{"name":"8b"},"id":583856605,"isCurrent":true,"startDate":{"month":2,"year":2014},"summary":"Currently building a career platform for Cruited.\n\nRefined the Citizen Hive project requirements into specifications and UI mockups.\n\nFully managed, specified and implemented the Howl at Them web application used by Wolf PAC.","title":"Freelance Web Developer"}]},"publicProfileUrl":"https://www.linkedin.com/pub/christophe-bram/42/64a/26a","siteStandardProfileRequest":{"url":"https://www.linkedin.com/profile/view?id=AAoAAAj-cPIBk82OHE6XPJpm_QgKwVSl5hHf3KQ&authType=name&authToken=ry53&trk=api*a296796*s304357*"}}
它没有这样做的原因是,我的
JsValue
是一个
JsString
,而它应该是一个
JsObject

为什么你问的是
JsString
?因为它是以
Json.toJson(jsonString)
(糟糕!)的形式诞生的,而不是
Json.parse(jsonString)
(好!)