Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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;  
    });   
})
share|improve this question
    
In what way is your current code not working? – javabrett Jun 8 '15 at 6:45
    
It outputs nothing – temporalis Jun 8 '15 at 6:47
    
Looks like your JSON data is an array - have you tried obj.data[0].text.Menu1 – javabrett Jun 8 '15 at 6:50
    
unfortunately doesn't work – temporalis Jun 8 '15 at 7:11
up vote 1 down vote accepted

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;

share|improve this answer
    
That brings me the following in the console: "TypeError: Cannot read property 'text' of undefined" – temporalis Jun 8 '15 at 7:08
    
and I had to add this agan to not get an error because of obj: var obj = {content:null}; $scope.all = obj; – temporalis Jun 8 '15 at 7:10
1  
sorry my fault, in the meanwhile the json got corrupted, now it works – temporalis Jun 8 '15 at 7:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.