2

我正在尝试从 gdata json 中提取数据。然而,对象的名称有美元符号:

{
   "id":{
      "$t":"tag:youtube.com,2008:video:xedvi0LiZDA"
   },
   "media$group":{
      "media$category":[
         {
            "$t":"Shows",
            "label":"Shows",
            "scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/categories.cat"
         },
         {
            "$t":"6",
            "scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/releasemediums.cat"
         },
         {
            "$t":"3",
            "scheme":"http:\/\/gdata.youtube.com\/schemas\/2007\/mediatypes.cat"
         }
      ],
      "media$content":[
         {
            "url":"http:\/\/www.youtube.com\/v\/xedvi0LiZDA?version=3&f=user_uploads&app=youtube_gdata",
            "type":"application\/x-shockwave-flash",
            "medium":"video",
            "isDefault":"true",
            "expression":"full",
            "duration":204,
            "yt$format":5
         },
         {
            "url":"rtsp:\/\/v8.cache5.c.youtube.com\/CigLENy73wIaHwkwZOJCi2_nxRMYDSANFEgGUgx1c2VyX3VwbG9hZHMM\/0\/0\/0\/video.3gp",
            "type":"video\/3gpp",
            "medium":"video",
            "expression":"full",
            "duration":204,
            "yt$format":1
         },
         {
            "url":"rtsp:\/\/v6.cache7.c.youtube.com\/CigLENy73wIaHwkwZOJCi2_nxRMYESARFEgGUgx1c2VyX3VwbG9hZHMM\/0\/0\/0\/video.3gp",
            "type":"video\/3gpp",
            "medium":"video",
            "expression":"full",
            "duration":204,
            "yt$format":6
         }
      ],
      "media$credit":[
         {
            "$t":"epicnetworkmusic",
            "role":"uploader",
            "scheme":"urn:youtube",
            "yt$display":"EpicNetworkMusic",
            "yt$type":"partner"
         }
      ],
      "media$description":{
         "$t":"Our Facebook: http:\/\/on.fb.me\/oW4u8e & T-Shirts: http:\/\/bit.ly\/JSJebo\n---------------------------------------------------------------------------------------------------------------\nComing out on the 'Believe in Music Foundation' album on the 15th October\nhttp:\/\/www.facebook.com\/pages\/Believe-in-Music-Foundation\/130288053775923\n\nSystem\nFacebook: https:\/\/www.facebook.com\/systemdance\nSoundcloud: http:\/\/soundcloud.com\/systemdance\nYouTube: http:\/\/www.youtube.com\/user\/officialsystemuk\n---------------------------------------------------------------------------------------------------------------\nAll the artwork & design is owned by Mike Nesbitt\n\nDeviantArt: http:\/\/vashperado.deviantart.com\/\nTumblr: http:\/\/vashito.tumblr.com\/\nTwitter: https:\/\/twitter.com\/vashperado \n\nWallpaper Download: \nhttp:\/\/vashperado.deviantart.com\/art\/s-p-a-c-e-o-u-t-264782890\n---------------------------------------------------------------------------------------------------------------\nEpic Network:\nFacebook FanPage: http:\/\/on.fb.me\/h2ubeH\nTwitter: http:\/\/bit.ly\/i4iqZa\nGoogle+ http:\/\/goo.gl\/uO7n1\nSoundcloud: http:\/\/bit.ly\/e1Xukr\n\nTrack's title: System - Circles\n---------------------------------------------------------------------------------------------------------------\nSend demos to demo@epicnetworkrecords.com\nfor a possible release or get your track featured on ENR!\n\nWe'll get back to you if we like what we hear!\n---------------------------------------------------------------------------------------------------------------",
         "type":"plain"
      },
      "media$keywords":[

      ],
      "media$license":{
         "$t":"youtube",
         "type":"text\/html",
         "href":"http:\/\/www.youtube.com\/t\/terms"
      },
      "media$player":{
         "url":"http:\/\/www.youtube.com\/watch?v=xedvi0LiZDA&feature=youtube_gdata_player"
      },
      "media$thumbnail":[
         {
            "url":"http:\/\/i.ytimg.com\/vi\/xedvi0LiZDA\/default.jpg",
            "height":90,
            "width":120,
            "time":"00:01:42",
            "yt$name":"default"
         },
         {
            "url":"http:\/\/i.ytimg.com\/vi\/xedvi0LiZDA\/mqdefault.jpg",
            "height":180,
            "width":320,
            "yt$name":"mqdefault"
         },
         {
            "url":"http:\/\/i.ytimg.com\/vi\/xedvi0LiZDA\/hqdefault.jpg",
            "height":360,
            "width":480,
            "yt$name":"hqdefault"
         },
         {
            "url":"http:\/\/i.ytimg.com\/vi\/xedvi0LiZDA\/1.jpg",
            "height":90,
            "width":120,
            "time":"00:00:51",
            "yt$name":"start"
         },
         {
            "url":"http:\/\/i.ytimg.com\/vi\/xedvi0LiZDA\/2.jpg",
            "height":90,
            "width":120,
            "time":"00:01:42",
            "yt$name":"middle"
         },
         {
            "url":"http:\/\/i.ytimg.com\/vi\/xedvi0LiZDA\/3.jpg",
            "height":90,
            "width":120,
            "time":"00:02:33",
            "yt$name":"end"
         }
      ],
      "media$title":{
         "$t":"Drum & Bass - System - Circles",
         "type":"plain"
      },
      "yt$aspectRatio":{
         "$t":"widescreen"
      },
      "yt$duration":{
         "seconds":"204"
      },
      "yt$uploaded":{
         "$t":"2012-09-24T22:47:33.000Z"
      },
      "yt$uploaderId":{
         "$t":"UCvOGElQWhX8tyTxwzv1rKzg"
      },
      "yt$videoid":{
         "$t":"xedvi0LiZDA"
      }
   },
}

我使用的Javascript如下:

  $.ajax({
    url: 'api/video.php',
    success: function(data) {
      //$('.result').html(data);
      alert(data.media$group.yt$videoid.$t); 
    }
  });

我得到的错误是:Uncaught TypeError: Cannot read property 'yt$videoid' of undefined

4

1 回答 1

1

我需要将 json 响应解析为一个对象:

  var video = jQuery.parseJSON(data);
  alert(video['media$group']['yt$videoid']['$t']);  //media$group.yt$videoid.$t
于 2012-11-27T10:30:06.747 回答