- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个网站来显示一些加密货币。一些 其中我从 Coinmarkepcap API ( https://api.coinmarketcap.com/v1/ticker/ ) 获得。
我使用的nodeJS代码如下:
var https = require('https');
var optionsget = {
host : 'api.coinmarketcap.com',
port : 443,
path : '/v1/ticker/bitcoin',
method : 'GET'
};
var reqGet = https.request(optionsget, function(res) {
res.on('data', function(d) {
info = JSON.parse(d);
console.log(info);
});
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
API 返回以下数据:
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "2256.82",
"price_btc": "1.0",
...
"last_updated": "1496168353"
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "204.307",
"price_btc": "0.0902657",
...
"last_updated": "1496168366"
},
我收到以下错误:
SyntaxError: Unexpected token < in JSON at position 0
我注意到 API 的结果使用了括号 [],其中包含 JSON。
如何解析 JSON 数组,以便检索每种硬币的名称、价格、ID 等?
最佳答案
您必须替换:
var optionsget = {
host : 'api.coinmarketcap.com',
port : 443,
path : '/v1/ticker/bitcoin',
method : 'GET'
};
作者:
var optionsget = {
host : 'api.coinmarketcap.com',
port : 443,
path : '/v1/ticker/bitcoin/',
method : 'GET'
};
如果您不包含尾部斜杠,网站会将您重定向到带有尾部斜杠的 URL,并且 https.request 不会透明地处理重定向。
您应该检查回调中的 HTTP 状态代码 ( check the docs ):
var reqGet = https.request(optionsget, function(res) {
res.on('data', function(d) {
if(res.statusCode == 200) {
info = JSON.parse(d);
console.log(info);
} else {
/* Do something else */
console.log("!", res.statusCode);
}
});
});
关于javascript - 在nodeJS中解析Coinmarketcap JSON数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44267963/
下面是我为 coinmarketcap.com 提供的 API。我正在尝试提取当前价格并将其放入表格中。由于某种原因,此代码无法正常工作,我无法找出错误。非常感谢任何帮助。
只是通过 API 获取一些数据的简单测试是行不通的。我使用了他们自己网站上的 PHP 示例,但没有打印任何结果。来源:https://pro.coinmarketcap.com/api/v1#sect
只是通过 API 获取一些数据的简单测试是行不通的。我使用了他们自己网站上的 PHP 示例,但没有打印任何结果。来源:https://pro.coinmarketcap.com/api/v1#sect
Coinmarketcap 正在将他们的 API 更新到版本 2。你能帮我访问 JSON 数组结构吗,因为它与 API 版本 1 不同?数组路径是数据、#-of-crypto-token、报价、美元、
这个问题已经存在: How to fetch data from coinmarketcap API [duplicate] 关闭 3 年前。 我正在尝试从 coinmarketcap 获取数据,我
看起来信息是一个带有嵌入字典的列表。我是初学者,不完全了解如何从列表/字典中提取信息。 经验: 'data': [{ 'id': 1, 'name': 'Bitcoin',
我正在学习一个显示加密货币价格的应用教程,但我在使用新 API 时遇到了问题。下面的代码仅来自教程,导师使用的是旧的 coinmarketcap API。 Coinmarketcap 已经迁移到一个新
我只想获得例如的“price_usd”值比特币来自 https://coinmarketcap.com/api/我试过下面这段代码,但我不知道如何只获得这个值。我使用的是 Python 3.5.3。如
我正在尝试学习如何使用 Python、requests 和 BeautifulSoup 从 Coinmarketcap.com 网络抓取 BTC 历史数据。 我想解析以下内容: 1)日期 2)关闭 3
我正在尝试完成电报机器人,它会在几个命令后以消息响应......失去了尝试我可以单独解决这个问题的任何希望。那些带有预定义消息的命令已经完成,并且工作起来就像一个魅力。但现在我陷入了/price 命令
我正在尝试集成coinmarketcap api但无法真正获取数据。我注册了,获取了 API key ,并编写了以下方法来获取数据: let getPostsList = async () => {
这里没有编程经验...我一直在阅读一些与此相关的问题,但无法正确解决。我将 React 用于一个小型应用程序,并希望从 API(本例中为 Coinmarketcap)获取数据,但它需要一个 key ,
我需要使用 Java 从页面顶部提取货币市值(例如:市值:$306,020,249,332),请参阅附图。 我在Java Eclipse中使用了jsoup库,但没有提取卷。 Jsoup 仅提取其他属性
这里没有编程经验...我一直在阅读一些与此相关的问题,但无法正确解决。我将 React 用于一个小型应用程序,并希望从 API(本例中为 Coinmarketcap)获取数据,但它需要一个 key ,
我什至没有问题,甚至 刮 我定义的任意数量的页面,但它只显示每个页面的前 10 个结果 def scrape_pages(page_num): for page in range(1, page_nu
我已阅读 How to decode a nested JSON struct with Swift Decodable protocol?它没有解决我将字符串文字数字值用作根字典的特定用例。 还有
我正在使用 coinmarketcap api 用数据填充 google 工作表。 https://coinmarketcap.com/api/ 他们正在做出一些改变,这将使这变得困难。让我困惑的两个
我是一名优秀的程序员,十分优秀!