简体   繁体   中英

Import Specific Data From API?

I found this little "API" that I want to take data from.

https://www.bitstamp.net/api/ticker/

Basically, I want to take the value "ask" and use it in HTML. From the 7 values there, how do I specifically target the "ask" price and nothing else? I just want the number.

The API returns

{"high": "849.00", "last": "847.59", "timestamp": "1385491132", "bid": "847.59", "volume": "31642.03534404", "low": "770.10", "ask": "848.00"}

Sorry, I know I sound horribly dumb, I am still new to this.

That data is returned as JSON string, you can use JSON.parse to extract needed property.

Let's say you want to display value in this element:

<input type="text" id="ask" />

The code would be:

// simulated value, in reality result of API call
var sResult = '{"high": "849.00", "last": "847.59", "timestamp": "1385491132", "bid": "847.59", "volume": "31642.03534404", "low": "770.10", "ask": "848.00"}'

var jsResult = JSON.parse(sResult);

document.getElementById("ask").value = jsResult.ask;

Demo: http://jsfiddle.net/qfY6s/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM