javascript - 在angularjs中使用JSONObject读取json

标签 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,我在 Controller 中执行此函数:

$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;

关于javascript - 在angularjs中使用JSONObject读取json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28482840/

相关文章:

javascript - css - 动态改变伪元素的背景

javascript - HTML 无法引用 JavaScript 文件

json - Twitter user_timeline API 不显示转推

javascript - 如何使用 javascript 取消选中数组中的单选按钮

javascript - CoffeeScript/jQuery 语法可以更简单吗?

php - 自动回复 : Send in 1 Minute After Subscription SQL Statement

php - Codeigniter 加载不同的数据库配置

php - Symfony 2 KNP 菜单 : add CSS class to link

php - 如果列包含 Laravel 中的 JSON 编码数据,我如何在数据库中搜索值

javascript - Java解析对象