Atlas91
Atlas91

Reputation: 5904

Read json with JSONObject in angularjs

Following this tutorial: http://www.w3schools.com/angular/angular_http.asp i can parse a json perfectly but i've got a problem.. I have a php with a json in which inside there is a json something like this:

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

As you can see there is a JSONObject (item).. actually to parse a json without JSONObject i do this function inside my controller:

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

and then

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

But in my case i display nothing becase i have that "item" as JSONObject.. Any solution?

Upvotes: 0

Views: 106

Answers (1)

Satpal
Satpal

Reputation: 133433

Your problem is you are not using correct property in response JSON Object

Use

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

instead of

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

OR

$scope.names = response.item;

Upvotes: 3

Related Questions