【问题标题】:request node get results from api response请求节点从 api 响应中获取结果
【发布时间】:2017-05-01 20:55:16
【问题描述】:

我正在使用 request 进行 API 调用。

一切正常,但我实际上无法从响应中获得results

我的代码是这样的

app.get('/events/:query', (req, res) => {

    request('http://www.skiddle.com/api/v1/events/search/?api_key=*****&keyword=elvis', (error, response, body) => {
        res.setHeader('Content-Type', 'application/json');
        res.send(body);
    });

});

res.send(body); 如下所示。有谁知道我如何访问results?我试过body.results 但这不起作用。

{  
   "error":0,
   "totalcount":"25",
   "pagecount":20,
   "results":[  
      {  
         "id":"12958041",
         "EventCode":"FOOD",
         "eventname":"Elvis Tribute",
         "cancelled":"0",
         "venue":{  
            "id":72198,
            "name":"Simla Restaurant",
            "address":"5a Watling Street",
            "town":"Dordon",
            "postcode":"B78 1SS",
            "country":"GB",
            "phone":"00000000000",
            "latitude":52.597021,
            "longitude":-1.6113712,
            "type":"Restaurant",
            "rating":0
         },
         "imageurl":"https:\/\/d31fr2pwly4c4s.cloudfront.net\/2\/2\/a\/950766_0_elvis-tribute_th.jpg",
         "largeimageurl":"https:\/\/d31fr2pwly4c4s.cloudfront.net\/2\/2\/a\/950766_0_elvis-tribute.jpg",
         "link":"http:\/\/www.skiddle.com\/whats-on\/Birmingham\/Simla-Restaurant\/Elvis-Tribute\/12958041\/",
         "date":"2017-05-10",
         "startdate":"2017-05-10T19:00:00+00:00",
         "enddate":"2017-05-10T23:00:00+00:00",
         "description":"Elvis Tribute evening ",
         "openingtimes":{  
            "doorsopen":"19:00",
            "doorsclose":"23:00",
            "lastentry":""
         },
         "minage":"0",
         "imgoing":null,
         "goingtocount":"0",
         "tickets":false,
         "entryprice":"Ticket: GBP 26.95",
         "rep":{  
            "enabled":null
         }
      },
      {  
         "id":"12948859",
         "EventCode":"COMEDY",
         "eventname":"NCFComedy present The Elvis Dead - Part of Derby Comedy Fest",
         "cancelled":"0",
         "venue":{  
            "id":77454,
            "name":"Carnero Lounge",
            "address":"10 St Peter's Gate",
            "town":"Derby",
            "postcode":"DE1 1SH",
            "country":"GB",
            "phone":"",
            "latitude":52.9208847,
            "longitude":-1.4767019,
            "type":"bar",
            "rating":0
         },
         "imageurl":"https:\/\/d31fr2pwly4c4s.cloudfront.net\/5\/c\/c\/946899_2_ncfcomedy-present-the-elvis-dead-part-of-derby-comedy-fest_th.jpg",
         "largeimageurl":"https:\/\/d31fr2pwly4c4s.cloudfront.net\/5\/c\/c\/946899_2_ncfcomedy-present-the-elvis-dead-part-of-derby-comedy-fest.jpg",
         "link":"http:\/\/www.skiddle.com\/whats-on\/Derby\/Carnero-Lounge\/NCFComedy-present-The-Elvis-Dead---Part-of-Derby-Comedy-Fest\/12948859\/",
         "date":"2017-05-11",
         "startdate":"2017-05-11T19:00:00+00:00",
         "enddate":"2017-05-11T20:00:00+00:00",
         "description":"Rob Kemp: The Elvis Dead\r\nPart of Derby Comedy Festival",
         "openingtimes":{  
            "doorsopen":"19:00",
            "doorsclose":"20:00",
            "lastentry":""
         }
      }
   }

【问题讨论】:

    标签: javascript node.js ajax xmlhttprequest


    【解决方案1】:

    所以首先你需要解析你的身体。 尝试这个。

    try {
        body = JSON.parse(body);
    }
    catch(err) {
        console.log(err);
    }
    res.send(body.results);
    

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 2018-05-28
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      相关资源
      最近更新 更多