最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to extract price using javascript from coingecko API in a nested array - Stack Overflow

programmeradmin4浏览0评论

I've had success gathering price from cryptopare as below:

;tsyms=USD

returns:

{“USD”:0.996}

Notice how the USD price is not nested.

When trying to use coingecko's API, the price is nested. For example:

;vs_currencies=usd

returns:

{"bitcoin":{"usd":7238.46}}

My problem is that I can't figure out how to alter the javascript to locate the price.

The pertinent code is below:

$.getJSON(";vs_currencies=usd", function(data){
    $("#BTCPrice").text(data["usd"].toFixed(2));
}).fail(function( dat, textStatus, error ) {
    var err = textStatus + ", " + error;
    alert(err);
});

This code would work for the non-nested data, but not the nested. I'm sure it's a matter of adding something like

$(#BTCPrice").text(data["bitcoin":"usd"].toFixed)2));

But I just can't get the syntax right.

I've had success gathering price from cryptopare. as below:

https://min-api.cryptopare./data/price?fsym=GRIN&tsyms=USD

returns:

{“USD”:0.996}

Notice how the USD price is not nested.

When trying to use coingecko's API, the price is nested. For example:

https://api.coingecko./api/v3/simple/price?ids=bitcoin&vs_currencies=usd

returns:

{"bitcoin":{"usd":7238.46}}

My problem is that I can't figure out how to alter the javascript to locate the price.

The pertinent code is below:

$.getJSON("https://api.coingecko./api/v3/simple/price?ids=bitcoin&vs_currencies=usd", function(data){
    $("#BTCPrice").text(data["usd"].toFixed(2));
}).fail(function( dat, textStatus, error ) {
    var err = textStatus + ", " + error;
    alert(err);
});

This code would work for the non-nested data, but not the nested. I'm sure it's a matter of adding something like

$(#BTCPrice").text(data["bitcoin":"usd"].toFixed)2));

But I just can't get the syntax right.

Share Improve this question edited Dec 26, 2019 at 16:37 thingEvery 3,4341 gold badge21 silver badges25 bronze badges asked Dec 26, 2019 at 16:13 crrdlxcrrdlx 211 silver badge7 bronze badges 1
  • I am not sure where <code></code> e into play. If you were trying to indicated a section of text was code, just use 3 backticks at the start of a line. Complete it with 3 more back ticks. The second example has an object with a key of "bitcoin".. It's data is itself an object with a key of "usd" and value of 7238.46 – JGFMK Commented Dec 26, 2019 at 16:19
Add a ment  | 

2 Answers 2

Reset to default 4

You need to learn how to traverse the JSON data. Check this out.

In this case, you should be able to get at that value with something like:

data["bitcoin"]["usd"]

or

data.bitcoin.usd

Does this help you?

var dataString = "{\"bitcoin\":{\"usd\":7238.46}}";
var dataJSON = JSON.parse(dataString);
var bitcoinObject = dataJSON["bitcoin"];
console.log(bitcoinObject);
var curr = Object.keys(bitcoinObject)[0];
console.log(curr); // usd
console.log(dataJSON["bitcoin"][curr]); // 7238.46

发布评论

评论列表(0)

  1. 暂无评论