Javascript获取JSON数据的问题

时间:2014-11-26 06:49:57

标签: javascript jquery json

修复:我必须将文件上传到我的服务器才能运行ajax

所以现在我正在尝试从JSON文件中提取股票信息,但没有任何事情被提升。我提醒了ajax.status,结果显示0,所以我认为我的JSON文件没有被读取,因为我试图在同一目录中调用本地文件。

这是我的HTML代码:我应该提取3个股票报价。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Stock Quotes</title>
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="css/clean.css">
</head>
<body>
    <!-- Script 11.x - quote.html -->
    <p>Apple: $<span id="quote"></span></p>
    <p>Google: $<span id="quote"></span></p>
    <p>Facebook: $<span id="quote"></span></p>
    <script src="js/json2.js"></script>
    <script src="js/ajax.js"></script>
    <script src="js/quote.js"></script>
</body>
</html>

我的JS文件应该解析我的JSON文件然后拉出每个&#34; l&#34;从使用循环的对象我还没有创建。我正试图看看我现在是否可以拉出第一批苹果股票但是什么都没有出现。

window.onload = function() {
    'use strict';

    // Create the Ajax object:
    var ajax = getXMLHttpRequestObject();

    // Function to be called when the readyState changes:
    ajax.onreadystatechange = function() {

        // Check the readyState property:
        if (ajax.readyState == 4) {

            // Check the status code:
            if ( (ajax.status >= 200 && ajax.status < 300) 
            || (ajax.status == 304) ) {

                // Parse the response:
                var data = JSON.parse(ajax.responseText);

                // Update the page:

                var output = document.getElementById('quote');
                if (output.textContent !== undefined) {
                    output.textContent = data[0].l;
                } else {
                    output.innerText = data[0].l;
                }

            } // End of status IF.

        } // End of readyState IF.

    }; // End of onreadystatechange function.

    // Fetch the initial data:
    ajax.open('GET', 'resources/quote.json', true);
    ajax.send(null);



}; // End of onload anonymous function.

这是我正在解析的JSON文件。

[
    {
        "l": "117.60",
        "l_fix": "117.60",
        "l_cur": "117.60",
        "s": "2",
        "ltt": "4:04PM EST",
        "lt": "Nov 25, 4:04PM EST",
        "lt_dts": "2014-11-25T16:04:18Z",
        "c": "-1.03",
        "c_fix": "-1.03",
        "cp": "-0.86",
        "cp_fix": "-0.86",
        "ccol": "chr",
        "pcls_fix": "118.625",
        "el": "117.73",
        "el_fix": "117.73",
        "el_cur": "117.73",
        "elt": "Nov 25, 7:59PM EST",
        "ec": "+0.13",
        "ec_fix": "0.13",
        "ecp": "0.11",
        "ecp_fix": "0.11",
        "eccol": "chg",
        "div": "0.47",
        "yld": "1.60",
        "eo": "",
        "delay": "",
        "op": "119.07",
        "hi": "119.75",
        "lo": "117.45",
        "vo": "68.84M",
        "avvo": "52.32M",
        "hi52": "119.75",
        "lo52": "70.51",
        "mc": "693.53B",
        "pe": "18.29",
        "fwpe": "",
        "beta": "0.90",
        "eps": "6.43",
        "shares": "5.87B",
        "inst_own": "62%",
        "name": "Apple Inc.",
        "type": "Company"
    },
    {
        "l": "541.08",
        "l_fix": "541.08",
        "l_cur": "541.08",
        "s": "2",
        "ltt": "4:04PM EST",
        "lt": "Nov 25, 4:04PM EST",
        "lt_dts": "2014-11-25T16:04:18Z",
        "c": "+1.81",
        "c_fix": "1.81",
        "cp": "0.34",
        "cp_fix": "0.34",
        "ccol": "chg",
        "pcls_fix": "539.27",
        "el": "541.08",
        "el_fix": "541.08",
        "el_cur": "541.08",
        "elt": "Nov 25, 6:37PM EST",
        "ec": "0.00",
        "ec_fix": "0.00",
        "ecp": "0.00",
        "ecp_fix": "0.00",
        "eccol": "chb",
        "div": "",
        "yld": "",
        "eo": "",
        "delay": "",
        "op": "539.00",
        "hi": "543.98",
        "lo": "538.60",
        "vo": "1.78M",
        "avvo": "1.97M",
        "hi52": "604.83",
        "lo52": "502.80",
        "mc": "369.17B",
        "pe": "27.42",
        "fwpe": "",
        "beta": "",
        "eps": "19.73",
        "shares": "339.34M",
        "inst_own": "69%",
        "name": "Google Inc",
        "type": "Company"
    },
    {
        "l": "75.63",
        "l_fix": "75.63",
        "l_cur": "75.63",
        "s": "2",
        "ltt": "4:04PM EST",
        "lt": "Nov 25, 4:04PM EST",
        "lt_dts": "2014-11-25T16:04:19Z",
        "c": "+1.62",
        "c_fix": "1.62",
        "cp": "2.19",
        "cp_fix": "2.19",
        "ccol": "chg",
        "pcls_fix": "74.01",
        "el": "75.75",
        "el_fix": "75.75",
        "el_cur": "75.75",
        "elt": "Nov 25, 7:59PM EST",
        "ec": "+0.12",
        "ec_fix": "0.12",
        "ecp": "0.16",
        "ecp_fix": "0.16",
        "eccol": "chg",
        "div": "",
        "yld": "",
        "eo": "",
        "delay": "",
        "op": "74.21",
        "hi": "75.74",
        "lo": "74.13",
        "vo": "32.25M",
        "avvo": "38.87M",
        "hi52": "81.16",
        "lo52": "43.55",
        "mc": "206.14B",
        "pe": "71.76",
        "fwpe": "",
        "beta": "",
        "eps": "1.05",
        "shares": "2.04B",
        "inst_own": "69%",
        "name": "Facebook Inc",
        "type": "Company"
    }
]

3 个答案:

答案 0 :(得分:0)

你能尝试像这样解析json:

var data = eval('('+ajax.responseText+')');

答案 1 :(得分:0)

我认为您的数据已经从.json文件中收到了解析 您可以尝试检查ajax.responseText 请注意,JSON.parse接受参数作为字符串。即

JSON.parse("[{}]");//parsed successfully
JSON.parse([{}]);//error

答案 2 :(得分:0)

您正在通过ajax读取JSON文件,它将您的文件内容解析为JSON对象作为ajax响应,因此无需再次将其解析为JSON,您可以直接将响应用作JSON对象。

删除以下行:

 var data = JSON.parse(ajax.responseText);  

直接使用var data = ajax.responseText;