Javascript 访问JSON对象中的值

Javascript 访问JSON对象中的值,javascript,json,node.js,object,Javascript,Json,Node.js,Object,我有一个JSON对象event.body,它包含{“article\u url”:http://technewstt.com/bd1108/“}如何访问文章url的值?换句话说,我想使用字符串http://technewstt.com/bd1108/我尝试了event.body.article\u url和event.article\u url,它们都是未定义的使用中间件: const bodyParser = require('body-parser'); app.use(bodyParse

我有一个JSON对象
event.body
,它包含
{“article\u url”:http://technewstt.com/bd1108/“}
如何访问
文章url
的值?换句话说,我想使用字符串
http://technewstt.com/bd1108/

我尝试了
event.body.article\u url
event.article\u url
,它们都是
未定义的

使用中间件:

const bodyParser = require('body-parser');

app.use(bodyParser.json());

然后,您就可以了。

如果您确定
event.body
包含正确的数据,您可以使用json解析它并获取值和键

这相当于

JSON.parse(JSON.stringify(event.body), (key, value) => {
  console.log(key);  
  return value;      
})
其他选项,如user@Grégory NEUT commented,将返回url值

JSON.parse(event.body).article_url  //returns the url value
最佳方式:

var url = JSON.parse(event.body).article_url;

您确定
event.body
do以这种方式包含值吗?因为
event.body.article\u url
应该可以工作。@GrégoryNEUT是的,我确定,因为我在
event.body
上做了
控制台.log
,它显示内容
{“article\u url”:http://technewstt.com/bd1108/“}
在它里面
console.log(typeof event.body)
console.log(Object.keys)呢(event.body))
因此尝试
JSON.parse(event.body).article\u url
@GrégoryNEUT
JSON.parse(event.body).article\u url
工作感谢返回我需要的值,也感谢尝试
JSON.parse(event.body).article\u url
如果OP使用connect包,这是一个间接有效的建议。
var url = JSON.parse(event.body).article_url;