Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm having this little app, requesting the json file which works fine when using one level json, below code that works:

service:

angular
    .module ('myApp')
    .factory('Summary', function ($resource) {
        return $resource('summary.json');
    });

controller:

angular
    .module ('myApp')
    .controller ('summaryCtrl', ['$scope', 'poller','Summary', function ($scope, poller,Summary) {

        var poller1;
        //using angular-poller 
        poller1 = poller.get(Summary, {delay: 2000});
        poller1.promise.then(null, null, function (data) {
            $scope.summary = data;
            console.log(data);
        });
    }]);

json:

[
    {"channel": "aaa", "value": 13256},
    {"channel": "bbb", "value": 6598},
    {"channel": "ccc", "value": 245 },
    {"channel": "ddd", "value": 123},
    { "channel": "eee", "value": 956},
    { "channel": "fff", "value": 142}
]

output in console:

[Resource, Resource, Resource, Resource, Resource, Resource, $promise: Object, $resolved: true]

All that above works fine as I'm getting 6x Resource, as soon as I introduce multilevel json and amend the controller it is not working properly, below is new code where I'm trying to retrieve uk:

new json:

[
    {
        "uk": [
            {"channel": "aaa", "value": 13256},
            {"channel": "bbb", "value": 6598},
            {"channel": "ccc", "value": 245 },
            {"channel": "ddd", "value": 123},
            { "channel": "eee", "value": 956},
            { "channel": "fff", "value": 142}
        ]
    },
    {
        "us": [
            {"channel": "aaa", "value": 457},
            {"channel": "bbb", "value": 364},
            {"channel": "ccc", "value": 457 },
            {"channel": "ddd", "value": 45},
            { "channel": "eee", "value": 3},
            { "channel": "fff", "value": 143562}
        ]

    }
]

controller:

angular
        .module ('myApp')
        .controller ('summaryCtrl', ['$scope', 'poller','Summary', function ($scope, poller,Summary) {

            var poller1;
            poller1 = poller.get(Summary, {delay: 2000});
            poller1.promise.then(null, null, function (data) {
                $scope.summary = data.uk; //here I need to get uk data
                console.log(data.uk);
            });
        }]);

output in console is undefined

What am I doing wrong here? Many thanks for your help.

share|improve this question
1  
data is an array. You would have to do data[0].uk – KJ Price Aug 6 '14 at 13:21
    
Oh wow, silly me :) Many thanks for your help. However, what it would look my code like if data were a proper json format/objects rather than array? – angular_learner Aug 6 '14 at 13:25
    
It's still proper json, you would just remove the surrounding "[" and "]". So it would look like {"uk":[]} instead of [{"uk":[]}] – KJ Price Aug 6 '14 at 13:27
    
I've tried that by removing [] so it looks {"uk":[]},{"us":[]} but my IDE argue that there is an error which is comma in between them :( – angular_learner Aug 6 '14 at 13:37
    
Oh, I see. Sorry. Let me write up an answer for this. – KJ Price Aug 6 '14 at 13:42
up vote 0 down vote accepted

Change the format of your json to look like this:

{
    "uk": [
        {"channel": "aaa", "value": 13256},
        {"channel": "bbb", "value": 6598},
        {"channel": "ccc", "value": 245 },
        {"channel": "ddd", "value": 123},
        { "channel": "eee", "value": 956},
        { "channel": "fff", "value": 142}
    ],
    "us": [
        {"channel": "aaa", "value": 457},
        {"channel": "bbb", "value": 364},
        {"channel": "ccc", "value": 457 },
        {"channel": "ddd", "value": 45},
        { "channel": "eee", "value": 3},
        { "channel": "fff", "value": 143562}
    ]

}

Then data.uk and data.us will produce the desired results.

share|improve this answer
    
Brilliant, all I had to to work with your solution was to amend factory like this: return $resource('summary.json',{}, {'query': {method: 'GET', isArray: false}}); Many thank again! – angular_learner Aug 6 '14 at 13:58
    
Awesome! So glad it worked out. – KJ Price Aug 6 '14 at 14:00

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.