Access key within json object

0

I have the following query By means of a ajax I get this json

{"messageList":{"USER_DATA":{"nombre":"pablo","apellido":"santamarina"},"SESION_DATA":{"isUserLogin":"true"}}}

What I want to do now is to go through the objects within messageList and access the key of each one but ideally without a for in For now I have this code but I'm still not accessing the value I need

ServicesController.prototype.newMessageInvoice = function(dataSource)
{
    var msgList = dataSource.messageList;

    for (var message in msgList) {

        var message = msgList[message];


    }
}

I clarify, I need to access the values of the keys, so that the return is for example " USER_DATA " or " SESION_DATA ", since I have to evaluate what type that message is.

I understand that the way would be to do something like this:

Object.keys(message)[0]

but I'm not stopping correctly inside the object

    
asked by Pablo 23.12.2016 в 16:20
source

1 answer

2

Well, I hope it will help if someone runs into the same problem, I could solve it in the following way

ServicesController.prototype.newMessageInvoice = function(dataSource)
{
    var msgList = dataSource.messageList;
    var msgsKeys = Object.keys(msgList);

    for(var i=0; i< msgsKeys.length; i++)
    {
        alert(msgsKeys[i]);
        var msgType     = msgsKeys[i];
        var msgContent  = dataSource.messageList[msgType];
        alert(JSON.stringify(msgContent));

    }
}
    
answered by 23.12.2016 в 16:25