如何在使用请求模块时从javascript中的body对象获取键值对

时间:2017-09-11 10:13:07

标签: javascript json

我正在创建一个应用程序,我想在其中使用来自JSON的一些数据,这是由另一个js文件生成的。这是生成JSON的代码

var request = require('request');

module.exports = {
foo:
request('https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/c1d1e5d6-fe5c-42de-8713-60f272a3b63e?subscription-key=d3d3e4dfa8744be9b4ae47558df8fc5a&timezoneOffset=0&verbose=true&q=hey',function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred 
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received 
  console.log(body);
})
};

我对身体对象感兴趣,它给出了以下内容 -

{
  "query": "hey",
  "topScoringIntent": {
    "intent": "Help",
    "score": 0.500165462
  },
  "intents": [
    {
      "intent": "Help",
      "score": 0.500165462
    },
    {
      "intent": "None",
      "score": 0.10364107
    },
    {
      "intent": "SearchHotels",
      "score": 0.00249445555
    },
    {
      "intent": "ShowHotelsReviews",
      "score": 9.451727E-06
    }
  ],
  "entities": []
}

现在我想从topScoringIntent元素访问intent的值。那到另一个JS文件。我尝试使用body [1] .intend但它给出了undefined。 我是javascript的新手,需要非常基本的代码才能做到这一点。请对此提出一些建议。另外请告诉我这是否可以通过身体解析器解决,如果是,那么如何?

更新 - 这是我想使用body ['topScoringIntent']的代码.enign为全局。

require('dotenv-extended').load();

var builder = require('botbuilder');
var restify = require('restify');
var Store = require('./store');
var spellService = require('./spell-service');
var request = require('request');
var myJSON = require("JSON");
var fs = require('fs');
//var window = window;
var request = require("request");
var myJSON = require("JSON");

var globalVar = [];

// Setup Restify Server
var server = restify.createServer();

server.listen(process.env.port || process.env.PORT || 3978, function () {
    console.log('%s listening to %s', server.name, server.url);
});
// Create connector and listen for messages
var connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());



function getMyBody(url, callback) {
  request({
    url: 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/c1d1e5d6-fe5c-42de-8713-60f272a3b63e?subscription-key=d3d3e4dfa8744be9b4ae47558df8fc5a&timezoneOffset=0&verbose=true&q=hey',
    json: true
  }, function (error, response, body) {
    if (error || response.statusCode !== 200) {
      return callback(error || {statusCode: response.statusCode});
    }

    global.topScoringIntent = body['topScoringIntent'].intent;
    //if(body['topScoringIntent'].intent == 'Help');
    //console.log('yay');
    callback(null, body);  
  });

}

getMyBody('https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/c1d1e5d6-fe5c-42de-8713-60f272a3b63e?subscription-key=d3d3e4dfa8744be9b4ae47558df8fc5a&timezoneOffset=0&verbose=true&q=hey', function(err, body) {
  if (err) {
    console.log(err);
  } 
})

if(body['topScoringIntent'].intent == 'Help');
    console.log('success');

2 个答案:

答案 0 :(得分:0)

这应该对你有用

console.log(response.topScoringIntent.intent);

答案 1 :(得分:-1)

body.topScoringIntent.intent将返回'帮助'。

要全局使用它,您可以设置var:

var body = {
  "query": "hey",
  "topScoringIntent": {
    "intent": "Help",
    "score": 0.500165462
  },
  "intents": [
    {
      "intent": "Help",
      "score": 0.500165462
    },
    {
      "intent": "None",
      "score": 0.10364107
    },
    {
      "intent": "SearchHotels",
      "score": 0.00249445555
    },
    {
      "intent": "ShowHotelsReviews",
      "score": 9.451727E-06
    }
  ],
  "entities": []
}

var result = body.topScoringIntent.intent;

然后在其他地方使用结果:

console.log(result);