无法访问JSON属性值

Unable to access JSON property values

本文关键字:属性 JSON 访问      更新时间:2023-09-26

这让我抓狂,我知道我一定错过了一些非常简单的东西!

我有一个返回JSON对象的服务:

[{"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的属性,例如使用显示名字

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:

<p>{{testJson}}</p>
<p>{{testJson.firstName}}</p>

但由于某种原因,{{testJson.firstName}}显示为空白!

下面是一个plunker:http://plnkr.co/edit/VWJfcXTPaDut2jqB4W3o?p=preview

有什么想法吗?

$scope.testJsonArray,您需要通过索引获取元素,然后通过Object 获取属性

 {{testJson[0].firstName}}

Example

您的testJson似乎是一个数组。所以{{testJson[0].firstName}}应该做到这一点!