在json中访问数据?

时间:2013-01-13 16:15:34

标签: javascript html json parsing

我想从youtube api获取json响应中的一些元素: 我在剧本中得到了这个回复:

{
  "version": "1.0",
  "encoding": "UTF-8",
  "feed": {
    "xmlns": "http://www.w3.org/2005/Atom",
    "xmlns$media": "http://search.yahoo.com/mrss/",
    "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/",
    "xmlns$gd": "http://schemas.google.com/g/2005",
    "xmlns$yt": "http://gdata.youtube.com/schemas/2007",
    "id": {
      "$t": "http://gdata.youtube.com/feeds/api/videos"
    },
    "updated": {
      "$t": "2013-01-13T15:48:18.863Z"
    },
    "category": [
      {
        "scheme": "http://schemas.google.com/g/2005#kind",
        "term": "http://gdata.youtube.com/schemas/2007#video"
      }
    ],
    "title": {
      "$t": "Videos matching: humor",
      "type": "text"
    },
    "logo": {
      "$t": "http://www.youtube.com/img/pic_youtubelogo_123x63.gif"
    },
    "link": [
      {
        "rel": "alternate",
        "type": "text/html",
        "href": "http://www.youtube.com"
      },
      {
        "rel": "http://schemas.google.com/g/2005#feed",
        "type": "application/atom+xml",
        "href": "http://gdata.youtube.com/feeds/api/videos"
      },
      {
        "rel": "http://schemas.google.com/g/2005#batch",
        "type": "application/atom+xml",
        "href": "http://gdata.youtube.com/feeds/api/videos/batch"
      },
      {
        "rel": "self",
        "type": "application/atom+xml",
        "href": "http://gdata.youtube.com/feeds/api/videos?alt=json&q=humor&start-index=1&max-results=1&lr=es&orderby=published"
      },
      {
        "rel": "next",
        "type": "application/atom+xml",
        "href": "http://gdata.youtube.com/feeds/api/videos?alt=json&q=humor&start-index=2&max-results=1&lr=es&orderby=published"
      }
    ],
    "author": [
      {
        "name": {
          "$t": "YouTube"
        },
        "uri": {
          "$t": "http://www.youtube.com/"
        }
      }
    ],
    "generator": {
      "$t": "YouTube data API",
      "version": "2.1",
      "uri": "http://gdata.youtube.com"
    },
    "openSearch$totalResults": {
      "$t": 10595
    },
    "openSearch$startIndex": {
      "$t": 1
    },
    "openSearch$itemsPerPage": {
      "$t": 1
    },
    "entry": [
      {
        "id": {
          "$t": "http://gdata.youtube.com/feeds/api/videos/HAUdFeouzgM"
        },
        "published": {
          "$t": "2013-01-13T15:43:26.000Z"
        },
        "updated": {
          "$t": "2013-01-13T15:43:26.000Z"
        },
        "category": [
          {
            "scheme": "http://schemas.google.com/g/2005#kind",
            "term": "http://gdata.youtube.com/schemas/2007#video"
          },
          {
            "scheme": "http://gdata.youtube.com/schemas/2007/categories.cat",
            "term": "Film",
            "label": "Cine y animación"
          }
        ],
        "title": {
          "$t": "Chica Troll en dibujo: TiparracoSA (IrukoArt)",
          "type": "text"
        },
        "content": {
          "$t": "Canal del artista creador: www.youtube.com Si quieres ver camaras ocultas dale aqui: www.youtube.com Si quieres ver a la chica troll VS pervertidos aqui: www.youtube.com Canal principal --- (Bromas con camara oculta y humor) www.youtube.com Sigueme en Twitter: twitter.com Sigueme en Facebook: www.facebook.com",
          "type": "text"
        },
        "link": [
          {
            "rel": "alternate",
            "type": "text/html",
            "href": "http://www.youtube.com/watch?v=HAUdFeouzgM&feature=youtube_gdata"
          },
          {
            "rel": "http://gdata.youtube.com/schemas/2007#video.responses",
            "type": "application/atom+xml",
            "href": "http://gdata.youtube.com/feeds/api/videos/HAUdFeouzgM/responses"
          },
          {
            "rel": "http://gdata.youtube.com/schemas/2007#video.related",
            "type": "application/atom+xml",
            "href": "http://gdata.youtube.com/feeds/api/videos/HAUdFeouzgM/related"
          },
          {
            "rel": "http://gdata.youtube.com/schemas/2007#mobile",
            "type": "text/html",
            "href": "http://m.youtube.com/details?v=HAUdFeouzgM"
          },
          {
            "rel": "self",
            "type": "application/atom+xml",
            "href": "http://gdata.youtube.com/feeds/api/videos/HAUdFeouzgM"
          }
        ],
        "author": [
          {
            "name": {
              "$t": "TiparracoTV"
            },
            "uri": {
              "$t": "http://gdata.youtube.com/feeds/api/users/TiparracoTV"
            }
          }
        ],
        "gd$comments": {
          "gd$feedLink": {
            "rel": "http://gdata.youtube.com/schemas/2007#comments",
            "href": "http://gdata.youtube.com/feeds/api/videos/HAUdFeouzgM/comments",
            "countHint": 0
          }
        },
        "media$group": {
          "media$category": [
            {
              "$t": "Film",
              "label": "Cine y animación",
              "scheme": "http://gdata.youtube.com/schemas/2007/categories.cat"
            }
          ],
          "media$content": [
            {
              "url": "http://www.youtube.com/v/HAUdFeouzgM?version=3&f=videos&app=youtube_gdata",
              "type": "application/x-shockwave-flash",
              "medium": "video",
              "isDefault": "true",
              "expression": "full",
              "duration": 324,
              "yt$format": 5
            },
            {
              "url": "rtsp://v1.cache2.c.youtube.com/CiILENy73wIaGQkDzi7qFR0FHBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
              "type": "video/3gpp",
              "medium": "video",
              "expression": "full",
              "duration": 324,
              "yt$format": 1
            },
            {
              "url": "rtsp://v1.cache2.c.youtube.com/CiILENy73wIaGQkDzi7qFR0FHBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
              "type": "video/3gpp",
              "medium": "video",
              "expression": "full",
              "duration": 324,
              "yt$format": 6
            }
          ],
          "media$description": {
            "$t": "Canal del artista creador: www.youtube.com Si quieres ver camaras ocultas dale aqui: www.youtube.com Si quieres ver a la chica troll VS pervertidos aqui: www.youtube.com Canal principal --- (Bromas con camara oculta y humor) www.youtube.com Sigueme en Twitter: twitter.com Sigueme en Facebook: www.facebook.com",
            "type": "plain"
          },
          "media$keywords": {

          },
          "media$player": [
            {
              "url": "http://www.youtube.com/watch?v=HAUdFeouzgM&feature=youtube_gdata_player"
            }
          ],
          "media$thumbnail": [
            {
              "url": "http://i.ytimg.com/vi/HAUdFeouzgM/0.jpg",
              "height": 360,
              "width": 480,
              "time": "00:02:42"
            },
            {
              "url": "http://i.ytimg.com/vi/HAUdFeouzgM/1.jpg",
              "height": 90,
              "width": 120,
              "time": "00:01:21"
            },
            {
              "url": "http://i.ytimg.com/vi/HAUdFeouzgM/2.jpg",
              "height": 90,
              "width": 120,
              "time": "00:02:42"
            },
            {
              "url": "http://i.ytimg.com/vi/HAUdFeouzgM/3.jpg",
              "height": 90,
              "width": 120,
              "time": "00:04:03"
            }
          ],
          "media$title": {
            "$t": "Chica Troll en dibujo: TiparracoSA (IrukoArt)",
            "type": "plain"
          },
          "yt$duration": {
            "seconds": "324"
          }
        },
        "yt$statistics": {
          "favoriteCount": "0",
          "viewCount": "1"
        }
      }
    ]
  }
}

我想访问一些元素,如标题,标签和链接,以便在我的脚本中使用。我eval json并且我尝试访问元素,但是在数组内部和另一个json对象我认为并且无法使脚本工作:

   var datos=data.responseText;

    var respuesta = eval("(" + data.responseText + ")");

    alert(respuesta.entry.title);

此代码不起作用,我如何访问元素?

1 个答案:

答案 0 :(得分:1)

请尝试使用alert(respuesta.entry[0].title);entry是一个数组。

编辑同样正如我们在评论entry中所提到的那样,在json中更深层次。正确的表达式实际上是:alert(respuesta.feed.entry[0].title);。我建议你使用Online JSON Beautifier来使你的json变得更加容易理解。