JSON from EJS to JSON object in JS

做~自己de王妃 提交于 2019-12-06 00:10:12

问题


I am using EJS with Node.JS, and am passing a JSON object into it, but need to have access to it as a usable object in the page. I am getting the unexpected token o error from this:

var initData=JSON.parse(<%-JSON.stringify(list)%>);

I cant figure out whats wrong here.

This is what it looks like in the file when rendered:

var initData=JSON.parse([{"title":"South Hills Health System - University Health Center","adr":"200 Lothrop St,15213","coords":"40.441875,-79.960813","images":[],"tags":[],"_id":"51c0e9798384f40000000017"},{"title":"Bombay Food Market","adr":"4605 Centre Avenue, Pittsburgh, PA 15213, USA","coords":null,"images":[],"tags":["indian","groceries","ethnic","store"],"_id":"51c0519e02b7cbec73000002"}]);

回答1:


try this:

var initData = JSON.parse('<%-JSON.stringify(list)%>');

OR:

var initData = <%-JSON.stringify(list)%>;


来源:https://stackoverflow.com/questions/17184179/json-from-ejs-to-json-object-in-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!