0

I am doing a youtube api call, and I get back a var result = JSON.stringify(response, '', 2); which looks like :

{
    "kind": "youtube#searchListResponse",
    "pageInfo": {
            "totalResults": 1000000,
            "resultsPerPage": 5
        },
    "items": [
        {
            "id": {
                "kind": "youtube#video",
                "videoId": "DEne4AoX_RU"
            },
            "kind": "youtube#searchResult",
            "snippet": {
                "publishedAt": "2012-11-22T22:36:15.000Z",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/DEne4AoX_RU/default.jpg"
                    },
                    "medium": {
                        "url": "https://i.ytimg.com/vi/DEne4AoX_RU/mqdefault.jpg"
                    },
                    "high": {
                        "url": "https://i.ytimg.com/vi/DEne4AoX_RU/hqdefault.jpg"
                    }
                }
            }
        },
        {
            "id": {...}

The full object response returns correctly in my console but I want to retrieve thumbnails url and display it as an li-tagged html list So I tried first to fetch in a list all the snippet entries :

var obj = $.parseJSON(result);
$.each(obj, function() {
    output += this.snippet + + "<br/>";
});
console.log(output);

But I have an message in my console : Uncaught TypeError: Cannot read property 'length' of undefined. What am I missing ? Btw, I don't understand why there are still brackets in the json stringified result (if someone could advise some good doc to understand how to parse JSON, would be great:))

1
  • Why do you stringify the response ? Commented Feb 7, 2013 at 14:35

3 Answers 3

1

You should be looping over items:

$.each(obj.items, function() {
    output += this.snippet ...
});
Sign up to request clarification or add additional context in comments.

Comments

1

What you receive is JSON, you shouldn't stringify it.

Remove this line

var result = JSON.stringify(response, '', 2);

and simply do

var obj = $.parseJSON(response);

Comments

1
  1. you want to iterate over items,
  2. snippet is an object literal,
  3. + + is not valid javascript.

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.