-2

I want to read items using jquery in the following JSON test

{"apiVersion":"2.0",
 "data":{
    "updated":"2010-01-07T19:58:42.949Z",
    "totalItems":800,
    "startIndex":1,
    "itemsPerPage":1,
    "items":[
        {"id":"hYB0mn5zh2c",
         "uploaded":"2007-06-05T22:07:03.000Z",
         "updated":"2010-01-07T13:26:50.000Z",
         "uploader":"GoogleDeveloperDay",
         "category":"News",
         "title":"Google Developers Day US - Maps API Introduction",
         "description":"Google Maps API Introduction ...",
         "tags":[
            "GDD07","GDD07US","Maps"
         ],
         "thumbnail":{
            "default":"http://i.ytimg.com/vi/hYB0mn5zh2c/default.jpg",
            "hqDefault":"http://i.ytimg.com/vi/hYB0mn5zh2c/hqdefault.jpg"
         },
         "player":{
            "default":"https://www.youtube.com/watch?v\u003dhYB0mn5zh2c",
            "mobile":"https://m.youtube.com/details?v\u003dhYB0mn5zh2c"
         },
         "content":{
            "1":"rtsp://v5.cache3.c.youtube.com/CiILENy.../0/0/0/video.3gp",
            "5":"http://www.youtube.com/v/hYB0mn5zh2c?f...",
            "6":"rtsp://v1.cache1.c.youtube.com/CiILENy.../0/0/0/video.3gp"
         },
         "duration":2840,
         "aspectRatio":"widescreen",
         "likeCount":171,
         "rating":4.63,
         "ratingCount":68,
         "viewCount":220101,
         "favoriteCount":201,
         "commentCount":22,
         "status":{
            "value":"restricted",
            "reason":"limitedSyndication"
         },
         "accessControl":{
            "syndicate":"allowed",
            "commentVote":"allowed",
            "rate":"allowed",
            "list":"allowed",
            "comment":"allowed",
            "embed":"allowed",
            "videoRespond":"moderated"
         }
        }
    ]
 }
}
3
  • So what's your question? You want an object or a string or something else? What have you tried yourself? Commented Aug 20, 2011 at 19:14
  • possible duplicate of How do I access variables in this JSON return using jQuery Commented Aug 20, 2011 at 19:16
  • I'm guessing the OP wants JSON.parse(s).data.items but who knows? I can't tell either.... Commented Aug 20, 2011 at 19:17

3 Answers 3

1

you can use jquery getJSON() jQuery Api

$.getJSON('ajax/test.json', function(jsonData) {
   alert(jsonData.data.items[0].id);
});
Sign up to request clarification or add additional context in comments.

4 Comments

how can I read "default" string for "thumbnail" object ??
alert(data.items[0].thumbnail.default);
I have this code : '$.getJSON(url,function(json){ $.each(json.data.items,function(i,tweet){ //txt='<p><img src="'+tweet.profile_image_url+'" widt="30" height="30" />'+tweet.text+'</p>'+txt; txt=txt+tweet.thumbnail.default+"<br>"; $("#youtube").html(txt);//append('<p><img src="'+tweet.profile_image_url+'" widt="30" height="30" />'+tweet.text+'</p>'); }); });' but the result is undefined !!
following sample work with your code $.getJSON(url, function (json) { $.each(json.data.items, function (i, tweet) { alert(tweet.thumbnail.default); }); }); you dont have text and profile_image_url in your json file
0

It looks like a well form json you can loop throught items array as below.

var jsonTest = {"apiVersion":"2.0",
 "data":{
    "updated":"2010-01-07T19:58:42.949Z",
    "totalItems":800,
    "startIndex":1,
    "items": []
...
..
};


$.each(jsonTest.items, function(index, item){
    alert(item.id);
    alert(item.uploaded);
   //In the loop "this" will also point to the current item from items array
});

Comments

0
  $.getJSON("pathto.json", function(json) {   

    alert(json.data.items[0].thumbnail.default);     //alerts http://i.ytimg.com/vi/hYB0mn5zh2c/default.jpg


 });

here is the fiddle http://jsfiddle.net/CSvjw/83/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.