How to traverse JSON with AJAX

2

I have the following JSON :

{"success":"true","data":{"usuarios":{"iduser":114,"cuenta":"arieldiaz"}}}

and my ajax is this:

success: function(response){
    if (response.success) {
        $.each(response.data.usuarios, function( key, value ) { 
            $.each( value, function (trkey,trvalue) {
                switch(trkey){ 
                    case 'iduser':
                    alert(trvalue);
                    break;
                }
            });
        });       
    }
}
  

The problem is that it does not take the value of the json (iduser)

    
asked by arglez35 01.08.2018 в 19:39
source

1 answer

2

You do not need the second $.each() . The first one already iterates through the users, you just have to display what you want. Something like this:

var response = {"success":"true","data":{"usuarios":{"iduser":114,"cuenta":"arieldiaz"}}};


var success = function(response){
    if (response.success) {
        $.each(response.data.usuarios, function( key, value ) {             
                switch(key){ 
                    case 'iduser':
                    alert(value);
                    break;
                }           
        });       
    }
};

success(response);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
answered by 01.08.2018 / 19:49
source