D
D
Dmitry2019-09-26 02:17:06
JavaScript
Dmitry, 2019-09-26 02:17:06

JSON parsing error - SyntaxError: unexpected token: numeric literal. What to do?

There is api https://api.bybit.com/v2/public/tickers , I'm trying to get the price of bitcoin from it result.0.last_price It doesn't work, browsers swear at 0 (zero) in the request. Here is the function code:
function GetBybitPrice() {
let zapros = new XMLHttpRequest();
zapros.open('GET', ' https://api.bybit.com/v2/public/tickers ');
zapros.onreadystatechange = function () {
if (this.readyState === 4) {
let answer = JSON.parse(zapros.responseText);
document.getElementById("bybit").innerText = response.result.0.last_price;
}
};
request.send();
}
Browsers don't digest otvet.result.0.last_price; because there is 0 in the string.
Firefox writes an error: SyntaxError: unexpected token: numeric literal
Chrome: Uncaught SyntaxError: Unexpected number
How to get to last_price then?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2019-09-26
@rd1706

otvet.result[0].last_price;
result is an array, treat it like an array.

D
Dmitry, 2019-09-26
@rd1706

I figured it out myself. I got confused in arrays and objects :))
The line should look like this:
answer.result[0].last_price;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question