Angularjs/Ionic App json parsing

temporalis

In an ionic/angularjs app I want to parse the content of the following json file (generated by a server)

 [
    {
        "raw": {
            "id": "2",
            "pid": "0",
            "sorting": "0",
            "tstamp": "1433706234",
            "Alias": "-2",
            "Menu1": "Test",
            "Menu2": "Test1",
            "From": "1433714400",
            "To": "1434060000",
            "Published": "1"
        },
        "text": {
            "Alias": "-2",
            "Menu1": "Test",
            "Menu2": "Test1",
            "From": "08.06.2015",
            "To": "12.06.2015",
            "Published": "1"
        },
        "attributes": {
            "Alias": "Alias",
            "Menu1": "Menu",
            "Menu2": "Menu",
            "From": "Datum From",
            "To": "Datum To",
            "Published": "Public"
        },
        "class": "first even"
    },
    {
        "raw": {
            "id": "1",
            "pid": "0",
            "sorting": "0",
            "tstamp": "1433706235",
            "Alias": "",
            "Menu1": "Test2",
            "Menu2": "Test21",
            "From": "1431295200",
            "To": "1431640800",
            "Published": ""
        },
        "text": {
            "Alias": "",
            "Menu1": "Test2",
            "Menu2": "Test21",
            "From": "11.05.2015",
            "To": "15.05.2015",
            "Published": ""
        },
        "attributes": {
            "Alias": "Alias",
            "Menu1": "Menu",
            "Menu2": "Menu",
            "From": "Datum From",
            "To": "Datum To",
            "Published": "Public"
        },
        "class": "last odd"
    }
]

I want to parse the data like this:

   .controller('GetJson', function ($scope, $http) {
        var obj = {content:null};      
        $http.get("test.json")
            .success(function (data) {
            obj.content = data;
        });
        return obj;
        $scope.all = obj;
        $scope.menu1 = obj.data.text.Menu1;
    });

And give it out:

<ion-view view-title="test" ng-controller="GetJson">
<ion-content class="padding">
    {{all}}
    {{menu1}}
</ion-content>
</ion-view>

But that's not working. How do I get the data from the json? For example if I want to get:

text->Menu1->"Test" or if I want to get attributes->Menu1->Menu

Thanks for help in advance

EDIT: It works with this:

.controller('GetJson', function ($scope, $http) {

    $http.get("test.json")
        .success(function (data) {
        $scope.all = data;    
        $scope.menu1 = data[0].text.Menu1;  
    });   
})
stian.net

Why do you use return statement in your controller? Why not just:

.controller('GetJson', function ($scope, $http) { 
        $scope.all = obj;
        $scope.menu1 = obj.data.text.Menu1; // this line won't work. See comment

        $http.get("test.json").success(function (data) {
            $scope.all = data;
        });
    });

I think maybe the controller in your code returns before the values for $scope.all and $scope.menu1 is set

And your objects are an array and "data" is not a property. So you can't access "obj.data.text.Menu1;". It should be obj[0].text.Menu1;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related