I am trying to use HTML and JavaScript to get some stock quotes from Yahoo and display them in a gadget for myself. I am querying the following URL, with an example symbol: https://query1.finance.yahoo.com/v7/finance/quote?symbols=aaba
The output is JSON or JSON-like, depending on where single- and double-quotes matter. TOR Browser recognizes the data as JSON-formatted.
My code can pull the data and receive it, but JSON.parse does not work on the output. Here's an example of the code I'm writing and loading with IE11 to test it before going to the gadget.
<!DOCTYPE html>
<html>
<body>
<p id="symbol"></p>
<p id="shortName"></p>
<p id="bid"></p>
<p id="debug"></p>
<script>
var objJSON;
var objXHR=new XMLHttpRequest();
objXHR.open("GET", "https://query1.finance.yahoo.com/v7/finance/quote?symbols=aaba");
objXHR.onreadystatechange=function() {
console.log(objXHR.status);
if (objXHR.readyState == 4 && objXHR.status == 200) {
console.log("Ready==4");
console.log(objXHR.responseText); //log the response
objJSON = JSON.parse(objXHR.responseText);
//objJSON = JSON.parse('{ "symbol":"aaba", "shortName":"Altaba", "bid":0}'); // Test data in properly-formatted JSON text
console.log("length " + Object.keys(objJSON).length); //indicator whether it parsed
document.getElementById("symbol").innerHTML = "symbol " + objJSON.symbol;
document.getElementById("shortName").innerHTML = "shortName " + objJSON.shortName;
document.getElementById("bid").innerHTML = "bid " + objJSON.bid;
}
};
objXHR.send(null);
</script>
</body>
</html>
The data doesn't have to be from Yahoo, but it does have to be something that I can get into a Windows Gadget and work, and be as simple as possible, because I'm not that advanced.
It looks like I just didn't understand the JSON object/array structure well enough. It looks like the string returned by Yahoo Finance is {object:{array[object:value]}}. I used console.log to march through the JSON structure in IE11, and eventually got there.