Using Mustache.js to Embed Raw JSON

[亡魂溺海] 提交于 2019-12-05 22:03:16
Jefferey Cave

A little bit old, but this was the first question that came up when I did a search. I found an answer here: https://stackoverflow.com/a/15580946/1961413

If you are able to preprocess the data, you could stringifiy it. This would make it a string that could be embedded:

var sampleData = JSON.stringify(sampleData);

Once you've done this you need to ensure that Mustache doesn't HTMLize the string (triple up the handlebars):

var reportObject = {{{.}}};

You can then access it in you webpage:

alert(reportObject.report_type);
for($data in reportObject.data){
     alert(JSON.stringify(reportObject.data[$data]));
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!