Get element in a json array

0

I need to get the authorDisplayName element of this array json . It is the return of a ajax request. How do I get only the element with jquery or javascript ?

 "kind": "youtube#commentThread",
 "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/chdu9X44b3BNrN9QUEyKNGH_eiA\"",
 "id": "z12ts1iicov0ybbex22syzij4tuwyrxyk04",
 "snippet": {
  "channelId": "id",
  "videoId": "id",
  "topLevelComment": {
   "kind": "youtube#comment",
   "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/gKHFh_4gWxRa4aGZKqb5E1DJnww\"",
   "id": "z12ts1iicov0ybbex22syzij4tuwyrxyk04",
   "snippet": {
    "authorDisplayName": "Nome do autor",
    "authorProfileImageUrl": "imagem",
    "authorChannelUrl": "LINK",
    "authorChannelId": {
     "value": "UCLw8RgF4mQXkA_-ZCXAAyIQ"
    },
    "channelId": "UCIwspRtKNszHhIhl36gREjQ",
    "videoId": "VYw3eYIOJ08",
    "textDisplay": "",
    "textOriginal": "r",
    "canRate": true,
    "viewerRating": "none",
    "likeCount": 0,
    "moderationStatus": "likelySpam",
    "publishedAt": "2016-10-05T19:23:16.000Z",
    "updatedAt": "2016-10-05T19:23:16.000Z"
   }
  },
  "canReply": true,
  "totalReplyCount": 0,
  "isPublic": true
 }
}

My code is like this

$(document).on('click','.enviarComentario', function(){                                         
            var comentario = $('.comentbox').val();
            var id = $(this).attr('videoId');           
            $.ajax({
                    type:'post',            
                    url: 'enviarComentario.php',
                    data : { "comentario" : comentario, "idVideo" : id},
                    success: function (e) {                     
                        alert(e);
                        var res = e;
                        alert(res);
                        var authorDisplayName = res.snippet.channelId;
                        alert(authorDisplayName);
                        var comentarios = $('.comentariosLista').html();        
                        $('.comentariosLista').html("");
                        $('.comentariosLista').html('<div class="media">'+
                                '<div class="media-left">'+
                                    '<a href="#">'+
                                    '<img class="media-object" src="'+res.snippet.topLevelComment.snippet.authorProfileImageUrl+'" alt="...">'+
                                    '</a>'+
                                '</div>'+
                                '<div class="media-body">'+
                                    '<h4 class="media-heading">'+
                                        '<strong>'+e.snippet.topLevelComment.snippet.authorDisplayName+'</strong>'+
                                    '</h4>'+comentario+'</div></div>');                                                                  
                    },
                    error: function(){
                        alert('n deu comentario');
                    }
            }); 
        });
    
asked by anonymous 05.10.2016 / 21:44

1 answer

0

The path inside the object is snippet > topLevelComment > snippet > authorDisplayName . It does not look like an array, but an object. If this object is inside an array, then you have to iterate the array first and every iteration you can do as I have below where I called res to the object.

var authorDisplayName = res.snippet.topLevelComment.snippet.authorDisplayName;

var res = {
    "kind": "youtube#commentThread",
    "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/chdu9X44b3BNrN9QUEyKNGH_eiA\"",
    "id": "z12ts1iicov0ybbex22syzij4tuwyrxyk04",
    "snippet": {
        "channelId": "id",
        "videoId": "id",
        "topLevelComment": {
            "kind": "youtube#comment",
            "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/gKHFh_4gWxRa4aGZKqb5E1DJnww\"",
            "id": "z12ts1iicov0ybbex22syzij4tuwyrxyk04",
            "snippet": {
                "authorDisplayName": "Nome do autor",
                "authorProfileImageUrl": "imagem",
                "authorChannelUrl": "LINK",
                "authorChannelId": {
                    "value": "UCLw8RgF4mQXkA_-ZCXAAyIQ"
                },
                "channelId": "UCIwspRtKNszHhIhl36gREjQ",
                "videoId": "VYw3eYIOJ08",
                "textDisplay": "",
                "textOriginal": "r",
                "canRate": true,
                "viewerRating": "none",
                "likeCount": 0,
                "moderationStatus": "likelySpam",
                "publishedAt": "2016-10-05T19:23:16.000Z",
                "updatedAt": "2016-10-05T19:23:16.000Z"
            }
        },
        "canReply": true,
        "totalReplyCount": 0,
        "isPublic": true
    }
};
var authorDisplayName = res.snippet.topLevelComment.snippet.authorDisplayName;
alert(authorDisplayName);
    
05.10.2016 / 21:48