gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:20:19 29 4
gpt4 key购买 nike

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

看起来像这样:

{"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*"}}

linkedinProfile: play.api.libs.json.JsValue 对象在我的 Twirl 模板中可用。

但我无法设法将它作为 JavaScript 对象传递给 JavaScript Controller 。我正在尝试这样做:

<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
CR.MyController(
@Html(linkedinProfile.toString)
);
});
</script>

问题在于,这段代码是由 Play Framework 编译成的:

<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>

所以基本上,数据作为字符串传递(注意开始和结束处的 "),最重要的是,双引号包裹的 JSON 键和值使用 \.

相反,我想要这样:

<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>

最佳答案

直接在模板中调用 Controller 会破坏 MVC模式。

在你的 Controller 中,你通常会做类似的事情

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>

关于javascript - 在 Twirl 模板中将 Scala JsValue 转换为 JavaScript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33394343/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com