如何使用哈巴狗迭代 json 数组?
How to iterate json array using pug?
我需要使用 pug 遍历 json 数组。
使用这条路线
app.get('/drone', function(req, res, next) {
console.log(mqttClient.drone_gps_json)
res.render('drone', {
title: 'drone',
message: (mqttClient.drone_gps_json)
});
})
我有这个输出
[
'{"type": "Point", "coordinates": [5.068762643835973, 4.348367550295752]}',
'{"type": "Point", "coordinates": [5.230064273129623, 4.0352242534817835]}',
'{"type": "Point", "coordinates": [4.724948591970712, 4.93405260010674]}',
'{"type": "Point", "coordinates": [4.177566385080386, 5.875375587957874]}',
...
]
为了可视化 json 数组,我使用了这个 pug 文件:
html
head
title= title
body
h1= message
有效
但是,我想要一个像这样的项目符号列表:
* "type": "Point", "coordinates": [5.068762643835973, 4.348367550295752]
* "type": "Point", "coordinates": [5.230064273129623, 4.0352242534817835]
* ...
正如已经建议的那样我试过:
html
head
title= title
body
ul
each item in message
li= message
但我遇到了这个错误:
GET /drone 500 211.871 ms - 1111
Error: Failed to lookup view "error" in views directory "/webserver/views"
建议?
您可能会遇到问题,因为请求输出是一个 字符串列表 ,看起来像 JSON。但它并不是看起来的对象列表。
您可以使用 JSON.parse() to convert a string into a json object. You can also use the map method 对数组的每个字符串应用 JSON.parse。
这是一个例子:
var resp = [
'{"type": "Point", "coordinates": [5.068762643835973, 4.348367550295752]}',
'{"type": "Point", "coordinates": [5.230064273129623, 4.0352242534817835]}',
'{"type": "Point", "coordinates": [4.724948591970712, 4.93405260010674]}',
'{"type": "Point", "coordinates": [4.177566385080386, 5.875375587957874]}'
] ;
var formattedArr = resp.map( str => JSON.parse(str) );
console.log("list of strings : ",resp);
console.log("list of objects : ", formattedArr);
不知道能不能解决你的问题。希望对您有所帮助 ;)
我需要使用 pug 遍历 json 数组。
使用这条路线
app.get('/drone', function(req, res, next) {
console.log(mqttClient.drone_gps_json)
res.render('drone', {
title: 'drone',
message: (mqttClient.drone_gps_json)
});
})
我有这个输出
[
'{"type": "Point", "coordinates": [5.068762643835973, 4.348367550295752]}',
'{"type": "Point", "coordinates": [5.230064273129623, 4.0352242534817835]}',
'{"type": "Point", "coordinates": [4.724948591970712, 4.93405260010674]}',
'{"type": "Point", "coordinates": [4.177566385080386, 5.875375587957874]}',
...
]
为了可视化 json 数组,我使用了这个 pug 文件:
html
head
title= title
body
h1= message
有效
但是,我想要一个像这样的项目符号列表:
* "type": "Point", "coordinates": [5.068762643835973, 4.348367550295752]
* "type": "Point", "coordinates": [5.230064273129623, 4.0352242534817835]
* ...
正如已经建议的那样
html
head
title= title
body
ul
each item in message
li= message
但我遇到了这个错误:
GET /drone 500 211.871 ms - 1111
Error: Failed to lookup view "error" in views directory "/webserver/views"
建议?
您可能会遇到问题,因为请求输出是一个 字符串列表 ,看起来像 JSON。但它并不是看起来的对象列表。
您可以使用 JSON.parse() to convert a string into a json object. You can also use the map method 对数组的每个字符串应用 JSON.parse。
这是一个例子:
var resp = [
'{"type": "Point", "coordinates": [5.068762643835973, 4.348367550295752]}',
'{"type": "Point", "coordinates": [5.230064273129623, 4.0352242534817835]}',
'{"type": "Point", "coordinates": [4.724948591970712, 4.93405260010674]}',
'{"type": "Point", "coordinates": [4.177566385080386, 5.875375587957874]}'
] ;
var formattedArr = resp.map( str => JSON.parse(str) );
console.log("list of strings : ",resp);
console.log("list of objects : ", formattedArr);
不知道能不能解决你的问题。希望对您有所帮助 ;)