简体   繁体   English

如何使用ExpressJS处理JSON响应

[英]How to handle json response using expressjs

i am new in nodejs (using expressjs) and i am calling a rest api,i am getting response as json in console(with two keys "id" and "text") but now i want to get "text" from json response and want to pass in "assistant.ask",How can i do this ? 我是nodejs的新手(使用expressjs),我正在调用rest api,我正在控制台中以json的形式获得响应(带有两个键“ id”和“ text”),但是现在我想从json响应中获得“ text”想要传递“ assistant.ask”,我该怎么办?

Here is my code 这是我的代码

function (error, response, body) {
        if (!error && response.statusCode == 200) {
         console.log(body);  //working
         assistant.ask(body.text);  // not working            
        }
    else{
        console.log(error); 
        }

Since your response is array, you need to access first item: 由于您的回答是数组,因此您需要访问第一项:

function (error, response, body) {
    if (!error && response.statusCode == 200) {
         assistant.ask(body[0].text);     
    }
    else {
        console.log(error); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM