How to read json in angular1?

2

I have the following json

{"message": "457896","additionalData":{"google.message_id":"0:149534266","coldstart":false,"collapse_key":"com.ionicframework.lucasteste693113","foreground":true}}

How can I give an alert to show all information?

I'm trying to show you all the information I've tried to do:

.controller('principalController', function($http, $scope, $sce, $stateParams, $ionicScrollDelegate, $timeout, $rootScope, $cordovaPushV5) {
    $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, notification){
        // esse nao veio 
        alert("result: " + JSON.stringify(notification.message) + JSON.stringify(notification.additionalData.google.message_id));
        //{"message": "457896","additionalData":{"google.message_id":"0:149534266","coldstart":false,"collapse_key":"com.ionicframework.lucasteste693113","foreground":true}}
    });
})

And it did not work, could anyone help me?

    
asked by anonymous 21.05.2017 / 08:13

2 answers

0

Actually already working, the problem is this field google.message_id, because it has a dot in the middle. Here's how:

.controller('principalController', function($http, $scope, $sce, $stateParams, $ionicScrollDelegate, $timeout, $rootScope, $cordovaPushV5) {
    $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, notification){
        _not= JSON.parse(notification);
        alert("result: " + _not.message + _not.additionalData["google.message_id"]);
    });
}
    
22.05.2017 / 00:11
0

To show all the information just give a 'stringify' in the 'notification':

.controller('principalController', function($http, $scope, $sce, $stateParams, $ionicScrollDelegate, $timeout, $rootScope, $cordovaPushV5) {
    $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, notification){
        alert("notificação: " + JSON.stringify(notification));
    });
}

Edited: Try to give a parse before:

.controller('principalController', function($http, $scope, $sce, $stateParams, $ionicScrollDelegate, $timeout, $rootScope, $cordovaPushV5) {
    $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, notification){
        _not= JSON.parse(notification);
        alert("result: " + _not.message + _not.additionalData.google.message_id);
    });
}
    
21.05.2017 / 17:21