简体   繁体   English

如何访问JSON API响应中的嵌套信息?

[英]How do I access nested information in a JSON API response?

I am trying to build a simple CLI cryptocurrency tracker app. 我正在尝试构建一个简单的CLI加密货币跟踪器应用程序。 The application performs a successful API call and returns the following response: 该应用程序执行成功的API调用,并返回以下响应:

[ { exchange: 'binance',
    base: 'ADA',
    quote: 'BTC',
    price_quote: '0.00001663',
    timestamp: '2019-04-08T16:36:00Z' },
  { exchange: 'binance',
    base: 'ADX',
    quote: 'BTC',
    price_quote: '0.00003316',
    timestamp: '2019-04-08T16:35:00Z' },
...]

How do I access a specific object within the response? 如何访问响应中的特定对象? For example, how could I return the entire object where base: 'ADA ? 例如,如何将整个对象返回base: 'ADA

Here is the simple Axios call that returns the JSON response: 这是返回JSON响应的简单Axios调用:

axios.get("https://api.nomics.com/v1/exchange-markets/prices?key=" + apiKey + "&currency=BTC&exchange=binance")
    .then(function (response) {
      console.log(response.data)
    })

You can use find 您可以使用查找

 let response = [ { exchange: 'binance',base: 'ADA',quote: 'BTC',price_quote: '0.00001663',timestamp: '2019-04-08T16:36:00Z' }, {exchange: 'binance', base: 'ADX',quote: 'BTC',price_quote: '0.00003316',timestamp: '2019-04-08T16:35:00Z' },] let value = response.find(e => e.base === 'ADA') console.log(value) 

axios.get("https://api.nomics.com/v1/exchange-markets/prices?key=" + apiKey + "&currency=BTC&exchange=binance")
.then(function (response) {
  console.log(response.data.find(data=>data.base==='ADA'))
}

The “find” function will go through every item of the array until it finds the item matches the provided boolean condition and returns it. “查找”功能将遍历数组的每个项目,直到找到与提供的布尔条件匹配的项目并返回它。

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

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