-3

How to get specific value from this JSON string. I want to get nested nested JSON object "FromUserId": "bd079f57" and pass it to Javascript function.

"notification":{
       "title" :"Test",
       "message" : "Message",  
           "android": {
                 "data": {
                         "priority": "2",
                        "profile": "profile",
                        "message": "xxx has sent you a Message.",
                        "style": "inbox",
                        "noteId": "1",
                        "visibility": "1",
                        "title": "Test",
                        "badge": "12",                  
                        "FromUserId": "bd079f57"                            
                        }
                    }
                 }



 "onNotification": function (notification) {         
                        var obj = JSON.parse(notification.message.android.data.FromUserId);  
                      // How to pass this test.FromUserId to ReadData function.                     
                      ReadData(obj)
                    }
$scope.ReadData(obj){
 //further process of json
}
Pritish
  • 2,194
  • 2
  • 21
  • 44
  • var obj = JSON.parse(test); ReadData(test.notification.android.data.FromUserId); Though im not 100% sure your json is valid, I assume it isn't actually set out like above. – Liam MacDonald Jul 31 '17 at 10:31
  • 1
    Are you getting string ? if yes then parse it like `JSON.prase(_your_json)` and then `notification.android.data.FromUserId` ? – user1608841 Jul 31 '17 at 10:31
  • JSON.parse(notification.FromUserId) should be JSON.parse(notification) – James Jul 31 '17 at 10:43
  • I dont understand why people are down voting this question? – Pritish Jul 31 '17 at 12:37

2 Answers2

1

JSON.stringify converts a JavaScript data structure into a string.

test.FromUserId attributes to read the FromUserId property of an object.

It isn't clear what the input to your function is, but you need to either:

  1. (If you are passing an object): Forget about JSON entirely. Remove all your JSON related functions. or
  2. (If you are passing a string of JSON): Use JSON.parse only, and use it only on notification.

Then you need to access through each layer of your data structure. You can't ignore all the objects between the top level and your data.

i.e. You need notification.message.android.data.FromUserId not test.FromUserId.

(In case 2 replace notification with test but you'll still need to explicitly access the message.android.data layers).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You can do like below :

"onNotification": function (notification) {         
                        var test = JSON.parse(notification);
                        var obj= test.notification.message.android.data.FromUserId;  
                      // How to pass this test.FromUserId to ReadData function.                     
                      ReadData(obj)
                    }
$scope.ReadData(obj){
 //further process of json
}
user1608841
  • 2,455
  • 1
  • 27
  • 40