0

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'.

4
  • 1
    topicDetails is not an array and so has no length property. $.each(value.topicDetails.topicIds) instead Commented Jun 18, 2014 at 8:01
  • I'm getting this: Cannot read property 'topicIds' of undefined Commented Jun 18, 2014 at 8:02
  • 1
    Works fine for me: jsfiddle.net/LC7Ju/1 Commented Jun 18, 2014 at 8:04
  • 1
    Looks like you are trying to get the length of an object key, rather than the value. Commented Jun 18, 2014 at 8:07

1 Answer 1

1

Read it like this:

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

fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.