使用jquery从json检索数据时出错

时间:2015-07-01 06:07:22

标签: javascript jquery json

我正在尝试从JSON检索数据并使用JQUERY在html中显示它。经过这么多不成功的尝试后,我发布了以下代码

HTML / Jquery的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>JSON</title>
</head>
<body>
    <div id="placeholder"></div>
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script>
  $.getJSON('file.json', function(data) {
        var output="<ul>";
        for (var i in data.reports) {
            output+="<li>" + data.reports.reportTitle[i] + " " + data.reports.reportDate[i] + "--" + data.reports.classificationType[i]+"</li>";
        }

        output+="</ul>";
        document.getElementById("placeholder").innerHTML=output;
  });
    </script>
</body>
</html>

我的Json的一部分

{
    "errmsg": null,
    "reportType": "criteriaReport",
    "totalRecNumbers": 918,
    "navigationControl": {
        "dimensionGroups": [
            {
                "dimensions": [
                    {
                        "refinements": [],
                        "dimName": "dimGeography",
                        "dimId": 4294289122,
                        "appendedURI": "/api/v2/criteriaReport?N=363&Ne=4294289122"
                    },
                    {
                        "refinements": [],
                        "dimName": "dimMarketSector",
                        "dimId": 4294288493,
                        "appendedURI": "/api/v2/criteriaReport?N=363&Ne=4294288493"
                    },
                    {
                        "refinements": [],
                        "dimName": "dimSectorPageMarketSectors",
                        "dimId": 4294281954,
                        "appendedURI": "/api/v2/criteriaReport?N=363&Ne=4294281954"
                    }
                ],
                "dimGroupName": "Filter All Results",
                "dimGroupId": 2
            },
            {
                "dimensions": [
                    {
                        "refinements": [],
                        "dimName": "dimReportType",
                        "dimId": 11,
                        "appendedURI": "/api/v2/criteriaReport?N=363&Ne=11"
                    }
                ],
                "dimGroupName": "Filter Research Results",
                "dimGroupId": 3
            },
            {
                "dimensions": [
                    {
                        "refinements": [],
                        "dimName": "dimResearchLanguage",
                        "dimId": 1214,
                        "appendedURI": "/api/v2/criteriaReport?N=363&Ne=1214"
                    }
                ],
                "dimGroupName": "Filter by Language",
                "dimGroupId": 6
            },
            {
                "dimensions": [
                    {
                        "refinements": [],
                        "dimName": "dimDateRange",
                        "dimId": 14,
                        "appendedURI": "/api/v2/criteriaReport?N=363&Ne=14"
                    }
                ],
                "dimGroupName": "Filter Results by Date",
                "dimGroupId": 4
            },
            {
                "dimensions": [
                    {
                        "refinements": [],
                        "dimName": "dimMarketFocus",
                        "dimId": 1233,
                        "appendedURI": "/api/v2/criteriaReport?N=363&Ne=1233"
                    }
                ],
                "dimGroupName": "",
                "dimGroupId": 0
            }
        ],
        "breadCrumbs": [
            {
                "dimensions": [
                    {
                        "refinements": [
                            {
                                "refinementId": 11,
                                "refinementName": "dimReportType",
                                "dimensionID": 11,
                                "appendURI": "N/A"
                            },
                            {
                                "refinementId": 363,
                                "refinementName": "Rating Criteria",
                                "dimensionID": 11,
                                "appendURI": "/api/v2/criteriaReport?N=363"
                            }
                        ],
                        "dimName": "dimReportType",
                        "dimId": 11,
                        "appendedURI": "N/A"
                    }
                ],
                "dimGroupName": "N/A"
            }
        ]
    },
    "pagingInfo": {
        "totalPageNumber": 19,
        "nextSets": null,
        "previousSets": null,
        "pages": [
            {
                "pageNum": 1,
                "subSetRange": "1 - 50",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=0"
            },
            {
                "pageNum": 2,
                "subSetRange": "51 - 100",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=50"
            },
            {
                "pageNum": 3,
                "subSetRange": "101 - 150",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=100"
            },
            {
                "pageNum": 4,
                "subSetRange": "151 - 200",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=150"
            },
            {
                "pageNum": 5,
                "subSetRange": "201 - 250",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=200"
            },
            {
                "pageNum": 6,
                "subSetRange": "251 - 300",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=250"
            },
            {
                "pageNum": 7,
                "subSetRange": "301 - 350",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=300"
            },
            {
                "pageNum": 8,
                "subSetRange": "351 - 400",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=350"
            },
            {
                "pageNum": 9,
                "subSetRange": "401 - 450",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=400"
            },
            {
                "pageNum": 10,
                "subSetRange": "451 - 500",
                "appendedURI": "/api/v2/criteriaReport?N=363&No=450"
            }
        ]
    },
    "reports": [
        {
            "reportTitle": "Criteria Addendum: Greece",
            "reportDate": "6/26/15 12:00 AM",
            "summary": "This report outlines Fitch Ratings assumptions for analysing securities backed by Greek residential mortgage loans.",
            "filePath": "/creditdesk/reports/report_frame.cfm?rpt_id=867293",
            "classificationType": "Sector Specific",
            "marketSectors": "Covered Bonds",
            "premium": true
        },
        {
            "reportTitle": "Global Credit Card ABS Rating Criteria",
            "reportDate": "6/26/15 12:00 AM",
            "summary": "This global criteria report describes Fitch Ratings methodology for analyzing securitizations of credit card receivables and may be applied to both international and national credit ratings. Global Credit Card ABS Rating Criteria is effective for new and existing ratings as of June 26, 2015.",
            "filePath": "/creditdesk/reports/report_frame.cfm?rpt_id=867482",
            "classificationType": "Sector Specific",
            "marketSectors": "Credit Cards - Prime, Credit Cards - Prime/Sub-Prime, Credit Cards - Retail, Credit Cards - Sub-Prime",
            "premium": true
        },

我正在尝试的是检索json文件中“reports”之后的字段。提前谢谢

1 个答案:

答案 0 :(得分:1)

由于data.reportsarray,您必须在data.reports上使用下标。

更改

data.reports.reportTitle[i] 

data.reports[i].reportTitle
//          ^^^  Should be here

更改for,如下所示:

for (var i in data.reports) {
    output += "<li>" + data.reports[i].reportTitle + " " + data.reports[i].reportDate + "--" + data.reports[i].classificationType + "</li>";
}