1

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;  
    });   
})
3
  • In what way is your current code not working? Commented Jun 8, 2015 at 6:45
  • Looks like your JSON data is an array - have you tried obj.data[0].text.Menu1 Commented Jun 8, 2015 at 6:50
  • unfortunately doesn't work Commented Jun 8, 2015 at 7:11

1 Answer 1

1

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;

Sign up to request clarification or add additional context in comments.

3 Comments

That brings me the following in the console: "TypeError: Cannot read property 'text' of undefined"
and I had to add this agan to not get an error because of obj: var obj = {content:null}; $scope.all = obj;
sorry my fault, in the meanwhile the json got corrupted, now it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.