How to read json in angular1?

Asked

Viewed 58 times

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 wanting to show all the information I tried to do so:

.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 but I don’t function someone could help me?

2 answers

0


Actually it is already working, the problem is this field google.message_id, because it has a point in the middle. Do it like this:

.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"]);
    });
}
  • I tried again with JSON.stringify and without it with this same I put, without the quotes but I do not work. No message appeared.

  • I made this example using the very json you posted: https://jsfiddle.net/joserogerio84/L51kpnr8/

  • Thank you so much for helping I was able to do it this way : Alert("result: " + JSON.stringify(notification.message) + JSON.stringify(notification.additionalData["google.message_id"])); And I work in Alert.

0

To show all information just give a 'stringify' on '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 a parse first:

.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);
    });
}
  • I am wanting to show all separate information put all separate values example : Alert("result: "+ JSON.stringify(notification.message) + JSON.stringify(notification.additionalData.google.message_id)); But when I put additionalData.google.message_id I’m not getting the value that’s coming from my json

  • Do you understand my doubt ? @joserogerio84

  • Got it, what comes out when you run: Alert("notification: " + JSON.stringify(notification)); ??

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

  • i managed to catch the message doing this : Alert("notification: " + JSON.stringify(notification.message)); but I’m not getting the rest of the message.

  • Try giving JSON.parse before Alert. https://www.w3schools.com/js/js_json_parse.asp

  • thus : JSON.parse(Alert("result: " + JSON.stringify(notification.message) + JSON.stringify(notification.additionalData))); ???

  • Now that I’ve added the parse that I speak is coming out this way : notification : {"message": "457896"{"google.message_id":"0:149534266" ,"coldstart":false," collapse_key":"com.i onicframework.lucast este693113","foregro und":true}}

  • and now how can I get this information coming from {} ? @joserogerio84

  • I tried to do so as you edit the answer : _not= JSON.parse(notification); Alert("result: " + JSON.stringify(_not.message) + JSON.stringify(_not.additionalData.google.message_id)); And I tried too without json.stringify But that way Alert didn’t show up

  • I tried to do so as you edit the answer : _not= JSON.parse(notification); Alert("result: " + JSON.stringify(_not.message) + JSON.stringify(_not.additionalData.google.message_id)); and I tried too without json.stringify But Alert did not appear with json.stringify and without it.

  • Would you have any other suggestions ? @joserogerio84

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.