使用NodeJS通过Https/GET请求获取Json数据并进行使用

要做的事情 zuò de shì

    • Json を返す Https な API を叩く

 

    返ってきたJsonをパースしてJSから利用する

你具体要做什么?

由于存在能够获取比特币汇率信息的API,我们可以利用它来获取当前的汇率信息。

交易所 API 摘要

返还的值

{
  "rate": "60000"
}

编码

const https = require('https');
// Bitcoin のレートを json で取得することができるAPI
const URL = "https://coincheck.com/api/rate/btc_jpy";

https.get(URL, function (res) {
    var body = '';
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        body += chunk;
    });
    res.on('data', function (chunk) {
        // body の値を json としてパースしている
        res = JSON.parse(body);
        console.log(`現在のレートは${res.rate}円/Bitcoinだよ!!`);
    })
  }).on('error', function (e) {
    console.log(e.message);
});

执行结果

2018-01-15_22-43-09[1].png
bannerAds