如何在名称中引用带有$(美元)符号的javascript对象

时间:2012-11-27 10:20:50

标签: javascript json jquery

我正在尝试从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:无法读取未定义的属性'yt $ videoid'

1 个答案:

答案 0 :(得分:1)

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

  var video = jQuery.parseJSON(data);
  alert(video['media$group']['yt$videoid']['$t']);  //media$group.yt$videoid.$t