试图解析一个Json,但未定义

时间:2017-11-07 14:25:28

标签: json node.js websocket

我正在使用一个简单的api请求来回复网站上的市场细节。
我能够在网站上获得交易对,但是我无法将费率保存到变量中。更别说能够单独打印出来了,因为我在尝试打印速率变量时一直未定义。

以下是我试图编写以收集此信息的代码。

var request = require('request');
request('https://api.quadrigacx.com/public/info',(err,response,payload) => {
try{

var json = JSON.parse(payload);
//console.log(json);
for( var market in json){
    var base = market.split('_')[0].toUpperCase();
    var quote = market.split('_')[1].toUpperCase(); 
    //var rate = json.market.rate; 
    var temp = market + ':'
    //console.log(rate + "\r\n");
    console.log(json.temp);
    //console.log(temp);

    }
}catch(err){
    console.log("QuadrigaCX parsing error");
    console.log(payload);
    }
});

如图所示打印出Json:

{ btc_cad: 
{ rate: '9126.99',
 volume: '455.29246286',
 sell: '9198.99',
 buy: '9145.00' },
 btc_usd: 
{ rate: '7135.00',
 volume: '27.94442250',
 sell: '7239.99',
 buy: '7135.00' },
eth_btc: 
{ rate: '0.04140000',
 volume: '3068.52782310',
 sell: '0.04187304',
 buy: '0.04102924' },
eth_cad: 
{ rate: '381.00',
 volume: '1710.79836900',
 sell: '383.00',
 buy: '380.55' },
ltc_cad: 
{ rate: '75.00',
 volume: '7151.43653780',
 sell: '75.00',
 buy: '74.01' },
 bch_cad: 
{ rate: '797.36',
 volume: '865.01121761',
 sell: '799.89',
 buy: '785.00' } }

1 个答案:

答案 0 :(得分:2)

要打印率,您需要这样做:

G���e���o���r���g���i���n���a��� ���l���o���v���e���s���