0

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"
        }
      }
    ]
  }
}

スクリプトで使用するために、タイトル、タグ、リンクなどの要素にアクセスしたいと考えています。私はjsonを評価し、要素にアクセスしようとしましたが、配列と別のjsonオブジェクトの中にあり、スクリプトを機能させることができません:

   var datos=data.responseText;

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

    alert(respuesta.entry.title);

このコードは機能しません。要素にアクセスするにはどうすればよいですか?

4

1 に答える 1

1

alert(respuesta.entry[0].title);代わりに試してください。entry配列です。

EDITまた、コメントで指摘したようentryに、json にはさらにもう 1 つのレベルがあります。実際の正しい表現は次のとおりalert(respuesta.feed.entry[0].title);です。Online JSON Beautifierを使用してJSON を美しくし、理解しやすくすることをお勧めします。

于 2013-01-13T16:17:25.973 に答える