我的问题非常简单。我有一个play.api.libs.json.JsValue
Scala对象通过Linkedin的REST API获得。
看起来像这样:
{"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*"}}
我的Twirl模板中有linkedinProfile: play.api.libs.json.JsValue
个对象。
但我无法将其作为JavaScript对象传递给JavaScript控制器。我试着这样做:
<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>
答案 0 :(得分:1)
直接在模板中调用控制器会破坏MVC模式。
在您的控制器中,您通常会执行类似
的操作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>
答案 1 :(得分:0)
@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)
(好!)。