javascript - Ajax 读取 JSON - 无法读取未定义的属性 'name'

标签 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)

谁能帮我理解为什么?

最佳答案

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

这将输出:A Overview

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

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

关于javascript - Ajax 读取 JSON - 无法读取未定义的属性 'name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52113215/

相关文章:

javascript - Google API 路径未显示

php - 将复选框值发送到 php 脚本以通过 jquery 进行处理

c# - 使用 XMLHttpRequest 的 ASP.NET AJAX 简单应用程序

javascript - 许多 100% 高度/宽度 div - 旋转木马导航?

arrays - PostgreSQL 难以从 jsonb 列中提取字段

javascript - 函数传递参数

java - LibGDX:从 json 文件读取到 ArrayList

javascript - 不同的浏览器对 jQuery ajax 调用得到不同的结果

javascript - 用户停止滚动时的事件

javascript - 使用鼠标单击/箭头键突出显示行