首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从数组访问JSON

无法从数组访问JSON
EN

Stack Overflow用户
提问于 2019-09-19 12:08:30
回答 1查看 43关注 0票数 0

我正在尝试使用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],但显然还有一些我不知道的其他问题。提前谢谢。

代码语言:javascript
复制
<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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-19 12:34:47

myObjdata属性是一个对象数组,因此要访问它的任何元素的price属性,必须使用如下所示的索引

代码语言:javascript
复制
myObj.data[0].price

这将为您提供data数组的第一个元素的价格。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58003598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档