Javascript 无法访问JSON属性值
这让我抓狂,我知道这一定是我错过的简单的东西 我有一个返回JSON对象的服务:Javascript 无法访问JSON属性值,javascript,json,angularjs,angularjs-scope,Javascript,Json,Angularjs,Angularjs Scope,这让我抓狂,我知道这一定是我错过的简单的东西 我有一个返回JSON对象的服务: [{"accountId":"0000004000006195","title":null,"firstName":"JOE","middleName":"BLOG","lastName":"BLOGGS","suffix":null,"primaryAddress":{"addressLine1":"TEST ADDRESS CLOSE","addressLine2":null,"addressLine3":"MY
[{"accountId":"0000004000006195","title":null,"firstName":"JOE","middleName":"BLOG","lastName":"BLOGGS","suffix":null,"primaryAddress":{"addressLine1":"TEST ADDRESS CLOSE","addressLine2":null,"addressLine3":"MY TOWN","town":"Washington","county":null,"country":"US","postcode":"PO9868"},"customerTelephones":[{"phoneNumber":"09876543455","type":"Alternate 1"},{"phoneNumber":"98654345676","type":"Alternate 2"}],"birthDate":108628400000}]
我试图使用以下方式显示JSON的属性,例如firstname:
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.testJson = [{"accountId":"0000004000006195","title":null,"firstName":"JOE","middleName":"BLOG","lastName":"BLOGGS","suffix":null,"primaryAddress":{"addressLine1":"TEST ADDRESS CLOSE","addressLine2":null,"addressLine3":"MY TOWN","town":"Washington","county":null,"country":"US","postcode":"PO9868"},"customerTelephones":[{"phoneNumber":"09876543455","type":"Alternate 1"},{"phoneNumber":"98654345676","type":"Alternate 2"}],"birthDate":108628400000}]
});
HTML:
{{testJson}
{{testJson.firstName}
但是由于某种原因,{{testJson.firstName}}显示为空白
这里有一个小插曲:
有什么想法吗?
$scope.testJson
是数组
您需要通过索引获取元素,然后通过对象获取属性
{{testJson[0].firstName}}
您的testJson似乎是一个数组。所以
{{testJson[0].firstName}
应该可以做到这一点
{{testJson[0].firstName}}