在angularjs中使用JSONObject读取json

时间:2015-02-12 16:36:14

标签: javascript php json angularjs

遵循本教程: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(item)..实际上解析一个没有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>

但在我的情况下,我没有显示任何因为我有这个&#34;项目&#34;作为JSONObject ..任何解决方案?

1 个答案:

答案 0 :(得分:3)

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

使用

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

而不是

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

$scope.names = response.item;