Node.js JSON提取某些数据

时间:2014-10-04 15:37:13

标签: json node.js

我试图从json链接获取某些数据: bittrex.com/api/v1.1/public/getticker?market=BTC-DRS

在我的节点IRC bot中使用: https://www.npmjs.org/package/node.bittrex.api

部分代码:

var url = ('https://bittrex.com/api/v1.1/public/getticker?market=BTC-DRS');

bittrex.options({
    'apikey' : settings.ticker.apikey,
    'apisecret' : settings.ticker.secretkey,
    'stream' : false,
    'verbose' : false,
    'cleartext' : true,
});
   case 'ticker':
    var user = from.toLowerCase();
    bittrex.sendCustomRequest(url, function(ticker, err) {
    if(err) {
      winston.error('Error in !ticker command.', err);
      client.say(channel, settings.messages.error.expand({name: from}));
      return;
    }
    winston.info('Fetched Price From BitTrex', ticker);
    client.say(channel, settings.messages.ticker.expand({name: user, price: ticker}));
   });
   break;

它工作但在IRC输出

[1:21am] <nrpatten> !ticker
[1:21am] <DRSTipbot> nrpatten The current DRS price at BitTrex {"success":true,"message":"","result":{"Bid":0.00000155,"Ask":0.00000164,"Last":0.00000155}}

我已经使用了一些东西来让它只显示&#34;最后&#34;从回复,但我一直在收到错误。

或从https://bittrex.com/api/v1.1/public/getmarketsummaries

获取某些数据

像我想要的任何信息一样:

{"MarketName":"BTC-DRS","High":0.00000161,"Low":0.00000063,"Volume":280917.11022708,"Last":0.00000155,"BaseVolume":0.33696054,"TimeStamp":"2014-10-04T15:14:19.66","Bid":0.00000155,"Ask":0.00000164,"OpenBuyOrders":33,"OpenSellOrders":138,"PrevDay":0.00000090,"Created":"2014-06-18T04:35:38.437"}

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

假设您已经解析了JSON(例如通过JSON.parse(str);),您只需使用您想要获得的任何属性名称。例如:

var info = JSON.parse('{"MarketName":"BTC-DRS","High":0.00000161,"Low":0.00000063,"Volume":280917.11022708,"Last":0.00000155,"BaseVolume":0.33696054,"TimeStamp":"2014-10-04T15:14:19.66","Bid":0.00000155,"Ask":0.00000164,"OpenBuyOrders":33,"OpenSellOrders":138,"PrevDay":0.00000090,"Created":"2014-06-18T04:35:38.437"}');
console.log(info.Bid);

此外,在不相关的问题上,通常回调参数遵循错误优先格式(例如(err, result)而不是(result, err)),以便与节点核心和npm上的大多数其他模块保持一致。 / p>