使用angularjs中的JSONObject读取json

Read json with JSONObject in angularjs

本文关键字:读取 json JSONObject 中的 angularjs 使用      更新时间:2023-09-30

遵循本教程:http://www.w3schools.com/angular/angular_http.asp我可以很好地解析json,但我遇到了一个问题。。我有一个带有json的php,其中有一个json,类似于以下内容:

{"item":[ 
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"City" : "Luleå",
"Country" : "Sweden"
}]}

正如您所看到的,有一个JSONObject(项)。。实际上,为了在没有JSONObject的情况下解析json,我在控制器中执行以下功能:

$http.get(JSONUrl)
        .success(function(response) {
            $scope.names = response;
        });

然后

<div data-ng-controller="MainController">
                <table class="uk-table uk-table-striped uk-table-hover" data-ng-if="!loading && !error">
                    <thead>
                    <th>Nome</th>
                    <th>Ver</th>
                    <th>Code</th>
                    </thead>
                    <tbody>
                    <tr data-ng-repeat="item in names">
                        <td>{{item.Name}}</td>
                        <td>{{item.City}}</td>
                        <td>{{item.Country}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>

但在我的情况下,我什么都不显示,因为我有那个"项目"作为JSONObject。。有什么解决方案吗?

您的问题是没有在响应JSON对象中使用正确的属性

使用

<tr data-ng-repeat="item in names.item">

而不是

<tr data-ng-repeat="item in names">

$scope.names = response.item;