我正在从 WordPress API V2 中选取数据,但某个键有冒号,导致操作中断。我正在使用 node.js 从 WordPress 在其 API 中返回的内容中选取特定数据。
下面是一个json对象
[
{
"id": 2873,
"title": {
"rendered": "some title"
},
"_embedded": {
"wp:featuredmedia": [ //i want to pich this in javascript
{
"id": 3091,
"date": "2018-04-18T20:51:42",
"slug": "dr",
"source_url": "some image url"
}
}
]
}
}
]
我正在使用 JavaScript NodeJS 来选择 wp:featuredmedia 内容,如下所示
app.get('/route', function (req, res) {
var array= [];
var url = 'url';
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
body.forEach(function (item) {
array.push({
picture: item._embedded.wp:featuredmedia //error is here
});
});
res.send(array);
}
})
});
最佳答案
使用方括号[]
属性访问器
app.get('/route', function (req, res) {
var array= [];
var url = 'url';
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
body.forEach(function (item) {
array.push({
picture: item._embedded["wp:featuredmedia"]
});
});
res.send(array);
}
})
});
关于javascript - 如何从 json 对象键中转义冒号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50086099/