2011-08-20 133 views
-2

我要在以下JSON測試使用jQuery如何使用jQuery解析這個JSON?

{"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" 
     } 
     } 
    ] 
} 
} 
+0

那麼什麼是你的問題?你想要一個對象或一個字符串或別的東西?你有什麼嘗試過自己? – Luwe

+0

可能的重複[如何使用jQuery訪問此JSON返回中的變量](http://stackoverflow.com/questions/3163417/how-do-i-access-variables-in-this-json-return-using- jQuery) –

+0

我猜OP想'JSON.parse(s).data.items'但是誰知道?我不能告訴任何.... –

回答

1

你可以使用jQuery getJSON()jQuery Api

$.getJSON('ajax/test.json', function(jsonData) { 
    alert(jsonData.data.items[0].id); 
}); 
+0

如何讀取「縮略圖」對象的「默認」字符串? – Adham

+0

'alert(data.items [0] .thumbnail.default);' –

+0

我有這個代碼:'$ .getJSON(url,function(json){ \t \t \t $ .each(json.data.items,function (I,鳴叫){ \t \t \t \t // TXT = '

'+ tweet.text +'

' + TXT; \t \t \t \t TXT = TXT + tweet.thumbnail.default + 「
」; \t \t (「#youtube」)。html(txt); // append('

'+ tweet.text +'

'); \t \t \t \t \t}); \t});'但結果是未定義的! – Adham

0

它看起來像一個很好的形式JSON閱讀items可以循環throught項目數組如下。

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 
}); 
0
$.getJSON("pathto.json", function(json) { 

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


}); 

這裏是小提琴http://jsfiddle.net/CSvjw/83/