我有一个文件向blockchain.info/ticker API发出请求,以便提取最新的比特币价格。
的script.js:
let request = require('request');
let url = "https://blockchain.info/ticker"
request.get(url,
function(error, response, body) {
console.log(body); //first
console.log(body.USD); //second
});
request.get中的第一个console.log语句返回:
{
"USD" : {"15m" : 1034.99, "last" : 1034.99, "buy" : 1033.63, "sell" : 1034.99, "symbol" : "$"},
"ISK" : {"15m" : 116090.37, "last" : 116090.37, "buy" : 115937.82, "sell" : 116090.37, "symbol" : "kr"},
"HKD" : {"15m" : 8043.46, "last" : 8043.46, "buy" : 8032.89, "sell" : 8043.46, "symbol" : "$"},
"TWD" : {"15m" : 31374.69, "last" : 31374.69, "buy" : 31333.46, "sell" : 31374.69, "symbol" : "NT$"},
"CHF" : {"15m" : 1030.89, "last" : 1030.89, "buy" : 1029.53, "sell" : 1030.89, "symbol" : "CHF"},
"EUR" : {"15m" : 963.89, "last" : 963.89, "buy" : 962.63, "sell" : 963.89, "symbol" : "€"},
"DKK" : {"15m" : 7170.6, "last" : 7170.6, "buy" : 7161.17, "sell" : 7170.6, "symbol" : "kr"},
"CLP" : {"15m" : 685784.37, "last" : 685784.37, "buy" : 684883.24, "sell" : 685784.37, "symbol" : "$"},
"CAD" : {"15m" : 1380.41, "last" : 1380.41, "buy" : 1378.6, "sell" : 1380.41, "symbol" : "$"},
"CNY" : {"15m" : 7131.48, "last" : 7131.48, "buy" : 7122.11, "sell" : 7131.48, "symbol" : "¥"},
"THB" : {"15m" : 35634.71, "last" : 35634.71, "buy" : 35587.88, "sell" : 35634.71, "symbol" : "฿"},
"AUD" : {"15m" : 1349.36, "last" : 1349.36, "buy" : 1347.58, "sell" : 1349.36, "symbol" : "$"},
"SGD" : {"15m" : 1443.9, "last" : 1443.9, "buy" : 1442.01, "sell" : 1443.9, "symbol" : "$"},
"KRW" : {"15m" : 1155773.33, "last" : 1155773.33, "buy" : 1154254.62, "sell" : 1155773.33, "symbol" : "₩"},
"JPY" : {"15m" : 115110.38, "last" : 115110.38, "buy" : 114959.12, "sell" : 115110.38, "symbol" : "¥"},
"PLN" : {"15m" : 4072.84, "last" : 4072.84, "buy" : 4067.49, "sell" : 4072.84, "symbol" : "zł"},
"GBP" : {"15m" : 832.11, "last" : 832.11, "buy" : 831.01, "sell" : 832.11, "symbol" : "£"},
"SEK" : {"15m" : 9212.32, "last" : 9212.32, "buy" : 9200.21, "sell" : 9212.32, "symbol" : "kr"},
"NZD" : {"15m" : 1474.6, "last" : 1474.6, "buy" : 1472.66, "sell" : 1474.6, "symbol" : "$"},
"BRL" : {"15m" : 3232.68, "last" : 3232.68, "buy" : 3228.43, "sell" : 3232.68, "symbol" : "R$"},
"RUB" : {"15m" : 58287.38, "last" : 58287.38, "buy" : 58210.79, "sell" : 58287.38, "symbol" : "RUB"}
}
第二个console.log语句返回:
undefined
我尝试了什么: 我试图将我在终端中获得的相同输出分配给chrome检查器中的变量,并访问不同的对象,以测试我的语法是否正确。语法是正确的,我真的很困惑。
为什么我的第二个console.log语句未定义,如何修复此问题以获得" 15m"美元对象内的价格?
答案 0 :(得分:1)
您是否测试过区分大小写? Javascript区分大小写,因此您可能必须使用
console.log(body.USD);
用我的一些json对象测试它,它看起来像那样
答案 1 :(得分:0)
以下是您的代码修复:
let request = require('request');
let url = "https://blockchain.info/ticker"
request.get(url, function(error, response, body) {
console.log(body); //first
console.log(JSON.parse(body).USD); // second
});
请求将响应作为JSON返回。
您必须使用JSON.parse
对其进行解析才能检索USD属性。
<强>输出强>
{
"USD" : {"15m" : 1034.66, "last" : 1034.66, "buy" : 1033, "sell" : 1034.66, "symbol" : "$"},
"ISK" : {"15m" : 116053.35, "last" : 116053.35, "buy" : 115867.16, "sell" : 116053.35, "symbol" : "kr"},
"HKD" : {"15m" : 8040.9, "last" : 8040.9, "buy" : 8028, "sell" : 8040.9, "symbol" : "$"},
"TWD" : {"15m" : 31364.68, "last" : 31364.68, "buy" : 31314.36, "sell" : 31364.68, "symbol" : "NT$"},
"CHF" : {"15m" : 1030.56, "last" : 1030.56, "buy" : 1028.91, "sell" : 1030.56, "symbol" : "CHF"},
"EUR" : {"15m" : 963.59, "last" : 963.59, "buy" : 962.04, "sell" : 963.59, "symbol" : "€"},
"DKK" : {"15m" : 7168.31, "last" : 7168.31, "buy" : 7156.81, "sell" : 7168.31, "symbol" : "kr"},
"CLP" : {"15m" : 685565.72, "last" : 685565.72, "buy" : 684465.8, "sell" : 685565.72, "symbol" : "$"},
"CAD" : {"15m" : 1379.97, "last" : 1379.97, "buy" : 1377.76, "sell" : 1379.97, "symbol" : "$"},
"CNY" : {"15m" : 7129.21, "last" : 7129.21, "buy" : 7117.77, "sell" : 7129.21, "symbol" : "¥"},
"THB" : {"15m" : 35623.34, "last" : 35623.34, "buy" : 35566.19, "sell" : 35623.34, "symbol" : "฿"},
"AUD" : {"15m" : 1348.93, "last" : 1348.93, "buy" : 1346.76, "sell" : 1348.93, "symbol" : "$"},
"SGD" : {"15m" : 1443.44, "last" : 1443.44, "buy" : 1441.13, "sell" : 1443.44, "symbol" : "$"},
"KRW" : {"15m" : 1155404.82, "last" : 1155404.82, "buy" : 1153551.1, "sell" : 1155404.82, "symbol" : "₩"},
"JPY" : {"15m" : 115073.68, "last" : 115073.68, "buy" : 114889.05, "sell" : 115073.68, "symbol" : "¥"},
"PLN" : {"15m" : 4071.54, "last" : 4071.54, "buy" : 4065.01, "sell" : 4071.54, "symbol" : "zł"},
"GBP" : {"15m" : 831.84, "last" : 831.84, "buy" : 830.51, "sell" : 831.84, "symbol" : "£"},
"SEK" : {"15m" : 9209.38, "last" : 9209.38, "buy" : 9194.6, "sell" : 9209.38, "symbol" : "kr"},
"NZD" : {"15m" : 1474.13, "last" : 1474.13, "buy" : 1471.76, "sell" : 1474.13, "symbol" : "$"},
"BRL" : {"15m" : 3231.65, "last" : 3231.65, "buy" : 3226.47, "sell" : 3231.65, "symbol" : "R$"},
"RUB" : {"15m" : 58268.79, "last" : 58268.79, "buy" : 58175.31, "sell" : 58268.79, "symbol" : "RUB"}
}
{ '15m': 1034.66,
last: 1034.66,
buy: 1033,
sell: 1034.66,
symbol: '$' }
答案 2 :(得分:0)
对象中没有属性“usd”。请尝试以下方法:
console.log(body.USD);
答案 3 :(得分:0)
console.log(body.USD);
//区分大小写
答案 4 :(得分:0)
实际上该财产是“美元”而不是usd。
尝试这个
console.log(body); //first
console.log(body.USD); //second