简体   繁体   中英

Parsing JSON within Jade

I have a big array of objects that I am passing via express into a Jade template. It looks like this:

[{ big object }, { big object }, { big object }, ...]

I pass it into the Jade template by stringifying it:

res.render('search-results', {
  data: JSON.stringify(body)
});

In my Jade template, I am trying to parse the JSON and iterate over each object within, as follows:

each d, i in JSON.parse(data)
  // Do stuff

However, d is logged as [object Obj] when I print it, and I am thus unable to access to object. When I try to do JSON.parse(d) , it also fails because d is literally the string "[object Obj]". I've tried passing the data into the template a bunch of different ways and keep coming up short. Any ideas?

Fixed by doing the following:

When constructing the array of objects on the backend Express-side, I stringified each of the objects inside of the array. Then, I passed the array of stringified JSON objects to the Jade template, which was consequently able to parse and use the data.

try each d, i in JSON.parse(data[0])

Since you send an array of objects you need to go one level deeper to come to the objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM