如何从Node.js中的JSON中提取消息值

时间:2017-11-21 04:24:49

标签: node.js

我的JSON是:

body = 
{  
"session_id":"45470003-6b84-4a2b-bf35-e850d1e2df8b",   
"message":"Thanks for calling service desk, may I know the store number you are calling from?", 
"callstatus":"InProgress", 
"intent":"",  
"responseStatusCode":null,  
"responseStatusMsg":null,  
 "context":"getstorenumber"  
}

如何使用节点js 获取消息值?测试后请告知我们 我尝试了 body.message body ['message'] 以及 body [0] ['message'] 。我总是得到“undefined”

1 个答案:

答案 0 :(得分:0)

@Chris评论,因为问题已解决,将答案添加到所有成员以供将来参考。 从node.js结果中,body被视为JSON字符串,使用转换为json对象的JSON.parse.body

body = 
{  
"session_id":"45470003-6b84-4a2b-bf35-e850d1e2df8b",   
"message":"Thanks for calling service desk, may I know the store number you are calling from?", 
"callstatus":"InProgress", 
"intent":"",  
"responseStatusCode":null,  
"responseStatusMsg":null,  
 "context":"getstorenumber"  
}

JSON.parse.body

console.log(body.message);