无法从数组访问JSON

时间:2019-09-19 04:08:29

标签: json

我正尝试使用https://www.worldtradingdata.com中的JSON使用JSON构建股票价格小部件

它的工作方式是我可以访问值,但不能从数组中访问值。 IE浏览器从这个演示中:

{"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" ...

我可以访问“消息”,但不能访问“价格”,这是我要显示的值。我尝试了三种不同的方法来实现此目的,请参阅[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

1 个答案:

答案 0 :(得分:0)

您的data的{​​{1}}属性是一个对象数组,因此要访问其任何元素的myObj属性,您必须使用类似

的索引
price

这将为您提供myObj.data[0].price 数组第一个元素的价格。