从POST响应中访问[Object]

时间:2016-11-02 22:13:35

标签: javascript json object httpresponse

如何访问[Object]并将每个对象的值返回到控制台?

响应:

[ 'qpxExpress#tripsSearch',
  { kind: 'qpxexpress#tripOptions',
    requestId: 'EWwQPoK0fma2Wgz1x0PNGc',
    data: 
     { kind: 'qpxexpress#data',
       airport: [Object],
       city: [Object],
       aircraft: [Object],
       tax: [Object],
       carrier: [Object] },
    tripOption: [ [Object] ] } ]

请求:

const options = {
  method: 'POST',
  uri: 'https://www.googleapis.com/qpxExpress/v1/trips/search?&key=XXXXXXXXXXXXXXXXX',
  body: {
  "request": {
    "passengers": {
      "adultCount": "1"
    },
    "slice": [
      {
        "origin": "SFO",
        "destination": "LAX",
        "date": "2017-09-19"
      }
    ],
    "solutions": "1"
  }
},
  json: true
}

1 个答案:

答案 0 :(得分:0)

只需要调用嵌套对象。 阅读api documentation帮助。

更新了返回总价格的代码:

request(options)
  .then(function (response) {
    console.log(response.trips.tripOption[0].saleTotal)
  })
  .catch(function (err) {
    console.log(err)
  })