Maxsteel
Maxsteel

Reputation: 2040

Read array in JSON object

I am trying to read following JSON:

{
   "items":[
      {
         "snippet":{
            "title":"Pharrell Williams - Happy (12AM)",
            "categoryId":"10"
         },
         "topicDetails":{
            "topicIds":[
               "/m/04mn81",
               "/m/0zdjzxm"
            ]
         }
      }
   ]
}

I've to read the TopicIds from the json. I've tried following to read the topicIds but it doesn't work. Please tell me what's wrong here:

  $.each(jsonResponse.items,function(key, value){
           $.each(value.topicDetails,function(k,v){
             for(var i=0, len = k.length; i< len; i++){
               alert(v[i]);
             }
           });
         });
}

It says there is no property `length'.

Upvotes: 0

Views: 60

Answers (1)

Fabricator
Fabricator

Reputation: 12772

Read it like this:

$.each(data.items, function(key, value) {
    $.each(value.topicDetails.topicIds, function(k, v) {
        console.log(k, v);
    });
});

fiddle

Upvotes: 1

Related Questions