我有一个JSON对象event.body
,其中包含{"article_url": "http://technewstt.com/bd1108/"}
,我如何访问article_url
的值,换句话说,我想使用字符串http://technewstt.com/bd1108/
。
我尝试了event.body.article_url
和event.article_url
,它们都是undefined
我有一个JSON对象event.body
,其中包含{"article_url": "http://technewstt.com/bd1108/"}
,我如何访问article_url
的值,换句话说,我想使用字符串http://technewstt.com/bd1108/
。
我尝试了event.body.article_url
和event.article_url
,它们都是undefined
如果您确定event.body
包含正确的数据,您可以使用JSON解析它并获取值和键
JSON.parse('{"article_url": "http://technewstt.com/bd1108/"}', (key, value) => {
console.log(key); //logs the key which is article_url
return value; // returns the url value
})
这相当于
JSON.parse(JSON.stringify(event.body), (key, value) => {
console.log(key);
return value;
})
其他选项,例如用户@Grégory NEUT的评论将返回URL值
JSON.parse(event.body).article_url //returns the url value
JSON.parse(event.body).article_url
返回的,也要感谢这个方法。 - Kern Elliott最佳实践:
var url = JSON.parse(event.body).article_url;
const bodyParser = require('body-parser');
app.use(bodyParser.json());
那么,你应该没问题了。
event.body
包含了那个值吗?因为event.body.article_url
应该是有效的。 - Orelsanplsevent.body
上执行了console.log
,它显示其中包含内容{"article_url": "http://technewstt.com/bd1108/"}
。 - Kern Elliottconsole.log(typeof event.body)
和console.log(Object.keys(event.body))
怎么样? - Orelsanplsconsole.log(typeof event.body)
返回string
,而console.log(Object.keys(event.body))
返回数字 0 到 47。 - Kern ElliottJSON.parse(event.body).article_url
。 - OrelsanplsJSON.parse(event.body).article_url
已经生效,谢谢。 - Kern Elliott