Ajax读取JSON-无法读取未定义的属性“名称”

时间:2018-08-31 10:12:47

标签: javascript json ajax

我的report.php返回一个json文件。这是我的JavaScript,可以尝试阅读:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function () {
        var data;
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: 'report.php',
            data: data,
            success: function (data) {
                console.log(data[0].name);
            }
        });
    });
</script>

JSON

{
    "report": {
        "type": "realtime",
        "elements": [{
            "id": "datetime",
            "name": "Date"
        }, {
            "id": "page",
            "name": "Page"
        }],
        "reportSuite": {
            "id": "myID",
            "name": "GD"
        },
        "period": "2018-08-31T08:31:26+0100\/2018-08-31T10:31:26+0100",
        "metrics": [{
            "id": "instances",
            "name": "Instances",
            "type": "number",
            "decimals": 0
        }],
        "data": [{
            "name": "2018-08-31T08:31:26+0100",
            "year": 2018,
            "month": 8,
            "day": 31,
            "hour": 8,
            "minute": 31,
            "breakdown": [{
                "name": ":A",
                "trend": "-91933.00",
                "counts": ["946801"]
            }, {
                "name": ":A Overview",
                "trend": "-97580.00",
                "counts": ["692229"]
            }, {
                "name": ":Successfull",
                "trend": "-39664.00",
                "counts": ["587378"]
            }, {
                "name": ":Trans",
                "trend": "-64227.00",
                "counts": ["440308"]
            }, {
                "name": ":Login",
                "trend": "-21233.00",
                "counts": ["367356"]
            }, {
                "name": ":Login - Enter Passcode",
                "trend": "-20456.00",
                "counts": ["334372"]
            }, {
                "name": ":Login - Complete",
                "trend": "-20724.00",
                "counts": ["321480"]
            }, {
                "name": ":Logiln - Complete",
                "trend": "-19448.00",
                "counts": ["291264"]
            }, {
                "name": "Li6",
                "trend": "-6278.00",
                "counts": ["100971"]
            }],
            "breakdownTotal": ["7118755"]
        }, {
            "name": "2018-08-31T09:31:26+0100",
            "year": 2018,
            "month": 8,
            "day": 31,
            "hour": 9,
            "minute": 31,
            "breakdown": [{
                "name": ":Accounts",
                "trend": "-91933.00",
                "counts": ["854868"]
            }, {
                "name": ":Overview",
                "trend": "-97580.00",
                "counts": ["594649"]
            }, {
                "name": "Li6",
                "trend": "-6278.00",
                "counts": ["94693"]
            }],
            "breakdownTotal": ["6613117"]
        }],
        "totals": ["13731872"],
        "version": "1.4.17.2"
    }
}

不是在控制台中打印json,而是给我这个错误:

Uncaught TypeError: Cannot read property 'name' of undefined
    at Object.success (index.php:23)
    at c (jquery-1.9.1.min.js:3)
    at Object.fireWith [as resolveWith] (jquery-1.9.1.min.js:3)
    at k (jquery-1.9.1.min.js:5)
    at XMLHttpRequest.r (jquery-1.9.1.min.js:5)

有人可以帮助我理解原因吗?

5 个答案:

答案 0 :(得分:2)

console.log(data.report.data[0].breakdown[1].name);

哪个将输出:A Overview

其中0是数据数组的第一个元素,而1是细分数组的第二个元素。也许您想使用.each()遍历数组?

https://api.jquery.com/jQuery.each/

答案 1 :(得分:1)

首先不要隐藏变量。
将您的数据重命名为clang(或类似名称)。
其次,在JSON中,您必须选择json的数据类型。 使用3

答案 2 :(得分:1)

看看JSON,我认为您缺少访问“报告”对象的权限,因此要获取第一个数据元素的名称是:

data.report.data[0].name

不只是

data[0].name

答案 3 :(得分:0)

应该像

Medium

答案 4 :(得分:0)

变量data包含整个响应对象。它不是数组。为什么不尝试这种方式。

data.report.elements[0].name