使用.length计算JSON中的项目

时间:2014-05-27 13:22:07

标签: json angularjs angularjs-scope angularjs-ng-repeat

我有一段JSON数据:

"links" : {
    "linkDetails" : [
        {
            "Name" : "Miss XYZ",
            "DateOfBirth" : "01/01/1901",
            "linkEntry" : [
                {
                    "Address": {
                        "HouseName": null,
                        "HouseNumber": "XX",
                        "PostCode": "XXX",
                        "Street": "XXXX",
                        "Town": "XXXXXX"
                    },
                    "Name" : "Mr ABC",
                    "DateOfBirth" : "10/06/1983",
                    "linkCurrent" : [
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "15/06/2012",
                            "ConfirmedBy" : "Joe Bloggs & Co"
                        },
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "15/06/2012",
                            "ConfirmedBy" : "Joe Bloggs & Co"
                        }
                       ],
                    "linkPrevious" : [
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "03/04/2012",
                            "ConfirmedBy" : "Lakers & Co"
                        },
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "03/04/2012",
                            "ConfirmedBy" : "Lakers & Co"
                        },
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "03/04/2012",
                            "ConfirmedBy" : "Lakers & Co"
                        }
                      ]
                },
                {
                    "Address": {
                        "HouseName": null,
                        "HouseNumber": "AA",
                        "PostCode": "AAAAA",
                        "Street": "AAAAAA",
                        "Town": "AAAAAA"
                    },
                    "Name" : "Mrs UYGTF",
                    "DateOfBirth" : "10/06/1964",
                    "linkCurrent" : [
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "15/06/2012",
                            "ConfirmedBy" : "Mont James"
                        },
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "15/06/2012",
                            "ConfirmedBy" : "Mont James"
                        }
                       ],
                    "linkPrevious" : [
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "03/04/2012",
                            "ConfirmedBy" : "PRAZLint"
                        }
                       ]
                },
                {
                    "Address": {
                        "HouseName": null,
                        "HouseNumber": "CC",
                        "PostCode": "CCCCC",
                        "Street": "CCCCCCCCC",
                        "Town": "CCCCC"
                    },
                    "Name" : "Mr HGF",
                    "DateOfBirth" : "10/06/1964",
                    "linkCurrent" : [
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "15/06/2012",
                            "ConfirmedBy" : "Aria"
                        }
                       ],
                    "linkPrevious" : [
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "03/04/2012",
                            "ConfirmedBy" : "MGM"
                        },
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "03/04/2012",
                            "ConfirmedBy" : "MGM"
                        },
                        {
                            "EntryNumber" : "XXX",
                            "Date" : "03/04/2012",
                            "ConfirmedBy" : "MGM"
                        }
                       ]
                }
            ]
        },

我有一个简单的ng-repeat,通过上面的循环显示NAME和DOB:

<tr ng-repeat-start="links in links">
    <td>{{links.linkDetails.Name}}</a></td>
    <td>(Date of Birth {{links.linkDetails.DateOfBirth}})</td>
    <td>{{links.linkDetails.linkEntry.linkCurrent.length }} current links found; {{links.linkDetails.linkEntry.linkPrevious.length }} previous links found</td>
</tr>

但是,当前和之前的length没有填充。

1 个答案:

答案 0 :(得分:1)

在您的JSON&#34; links&#34;是一个对象,&#34; linkDetails&#34;是一个数组和&#34; linkEntry&#34;也是一个数组。在你的angularjs代码&#34; links&#34;是一个数组&#34; linkDetails&#34;和&#34; linkEntry&#34;是对象。你需要解决这些问题。

你可能应该经历&#34; linkDetails&#34;不是&#34; links&#34;