我正在尝试使用https://www.worldtradingdata.com的JSON构建一个股票价格小部件
它的工作原理是,我可以访问值,但不能从数组中访问。即。来自此演示:
{"message":"This request is for demonstration purposes only. If you wish to use our API, please sign up and get your personal API token for free.","symbols_requested":3,"symbols_returned":3,"data":[{"symbol":"AAPL","name":"Apple Inc.","currency":"USD","price":"222.77" ...
我可以访问"message",但不能访问"price“,这是我想要显示的值。我已经尝试了三种不同的方法来实现这一点,参见[https://codepen.io/LF12/pen/oNvaQjL][1],但显然还有一些我不知道的其他问题。提前谢谢。
<html>
<body>
<h2>Use the XMLHttpRequest to get the content of a file.</h2>
<p>This content is imported from the JSON file at <a
href="https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo"
target="_blank">https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo</a></p>
This is the value from the name "message":
<p style="color: red" id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.message;
}
};
xmlhttp.open("GET", "https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo", true);
xmlhttp.send();
</script>
<hr>
<h2>Get value from inside array, take 1</h2>
<p>This content is imported from the JSON file at <a
href="https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo"
target="_blank">https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo</a> but
is inside an array. It returns 'undefined' instead of the value, "222.77".</p>
<p style="color: red" id="demo-2"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo-2").innerHTML = myObj.data.price;
}
};
xmlhttp.open("GET", "https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo", true);
xmlhttp.send();
</script>
<hr>
<h2>Take 2</h2>
<p>This content is imported from the JSON file at <a
href="https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo"
target="_blank">https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo</a> but
is inside an array. It returns 'undefined' instead of the value, "222.77".</p>
<p style="color: red" id="demo-3"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo-3").innerHTML = myObj.data["price"];
}
};
xmlhttp.open("GET", "https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo", true);
xmlhttp.send();
</script>
<hr>
<h2>Take 3</h2>
<p>This content is imported from the JSON file at <a
href="https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo"
target="_blank">https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo</a> but
is inside an array. It returns 'undefined' instead of the value, "222.77".</p>
<p style="color: red" id="demo-4"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var myArr = JSON.parse(this.responseText);
document.getElementById("demo-4").innerHTML = myArr[3];
}
};
xmlhttp.open("GET", "https://api.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=demo", true);
xmlhttp.send();
</script>
</body>
</html>
[1]: https://codepen.io/LF12/pen/oNvaQjL发布于 2019-09-19 12:34:47
myObj的data属性是一个对象数组,因此要访问它的任何元素的price属性,必须使用如下所示的索引
myObj.data[0].price这将为您提供data数组的第一个元素的价格。
https://stackoverflow.com/questions/58003598
复制相似问题