admin管理员组文章数量:1208155
When I send an object with an array of objects in it from my express route to my client, I get an [Object object] and then when I try to stringify it, I get this crazy string with this console message
var messages = "<%=(JSON.stringify(messages))%>"
console.log(messages)
Which prints this out to the console ...
{"messages":[{"content":"cool mane","creator":"joe"},{"content":"test 4","creator":"joe"},{"content":" ewgdqf","creator":"joe"},
It should be something so I can iterate through it by doing messages[0].content but I'm getting this crazy string that won't let me do anything with it...
If I try to loop through it, it just prints out each character by itself.
When I send an object with an array of objects in it from my express route to my client, I get an [Object object] and then when I try to stringify it, I get this crazy string with this console message
var messages = "<%=(JSON.stringify(messages))%>"
console.log(messages)
Which prints this out to the console ...
{"messages":[{"content":"cool mane","creator":"joe"},{"content":"test 4","creator":"joe"},{"content":" ewgdqf","creator":"joe"},
It should be something so I can iterate through it by doing messages[0].content but I'm getting this crazy string that won't let me do anything with it...
If I try to loop through it, it just prints out each character by itself.
Share Improve this question edited May 9, 2016 at 16:59 Felix Kling 816k180 gold badges1.1k silver badges1.2k bronze badges asked May 9, 2016 at 16:36 joejoe 1,7134 gold badges18 silver badges37 bronze badges 13 | Show 8 more comments2 Answers
Reset to default 21When using <%= ... %>
, EJS will encode / escape any output. That's why the "
in the JSON are encoded as "
. According to this answer, you can prevent escaping by using <%- ... %>
instead.
There is also no need to put the output inside a string literal. It's actually bad since you can get problems with nested quotes. Just let it output directly into the JS code:
var messages = <%-JSON.stringify(messages)%>;
Try to change this :
var messages = "<%=(JSON.stringify(messages))%>"
console.log(messages)
With this :
var messages = JSON.stringify("<%=messages%>");
console.log(messages)
本文标签: javascriptWhen I JSONstringify(object) I get a crazy string as a valueStack Overflow
版权声明:本文标题:javascript - When I JSON.stringify(object) I get a crazy string as a value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738734105a2109494.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<%= ... %>
is HTML encoding the output. Template engines often provide a way to output stuff without encoding it. Once you fixed that, you need toJSON.parse
the string as well. Still wondering whether there is a better way to pass the object to the client. – Felix Kling Commented May 9, 2016 at 16:37JSON.parse
. You have to use the non-encoding version of<%= ... %>
, whatever that is. Which template engine are you using? – Felix Kling Commented May 9, 2016 at 16:45var messages = <%-JSON.stringify(messages)%>;
. – Felix Kling Commented May 9, 2016 at 16:52