最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When I JSON.stringify(object) I get a crazy string as a value - Stack Overflow

programmeradmin0浏览0评论

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

{&#34;messages&#34;:[{&#34;content&#34;:&#34;cool mane&#34;,&#34;creator&#34;:&#34;joe&#34;},{&#34;content&#34;:&#34;test 4&#34;,&#34;creator&#34;:&#34;joe&#34;},{&#34;content&#34;:&#34; ewgdqf&#34;,&#34;creator&#34;:&#34;joe&#34;},

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

{&#34;messages&#34;:[{&#34;content&#34;:&#34;cool mane&#34;,&#34;creator&#34;:&#34;joe&#34;},{&#34;content&#34;:&#34;test 4&#34;,&#34;creator&#34;:&#34;joe&#34;},{&#34;content&#34;:&#34; ewgdqf&#34;,&#34;creator&#34;:&#34;joe&#34;},

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
  • 2 Seems like <%= ... %> is HTML encoding the output. Template engines often provide a way to output stuff without encoding it. Once you fixed that, you need to JSON.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:37
  • i don't think there's any other way to send data to the client, but JSON.parse gives me an error everytime I try to use it even after I stringify it – joe Commented May 9, 2016 at 16:43
  • 1 @T.J.Crowder that's just a typo from trying 100 different things I accidently kept them when I went back to my original code – joe Commented May 9, 2016 at 16:45
  • As you said, you need to fix the output before you can use JSON.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:45
  • 2 Uh, I'm stupid. Just do var messages = <%-JSON.stringify(messages)%>;. – Felix Kling Commented May 9, 2016 at 16:52
 |  Show 8 more comments

2 Answers 2

Reset to default 21

When using <%= ... %>, EJS will encode / escape any output. That's why the " in the JSON are encoded as &#34;. 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) 
发布评论

评论列表(0)

  1. 暂无评论