如何使用#Angular从具有相似名称的json嵌套结构访问数据

时间:2017-03-07 17:31:14

标签: angularjs json api

我正在学习角度,我遇到了一个问题,我想从json获取数据,但问题是来自json数据的嵌套数据在torrent下的类似名称是“URL”现在我想通过点击访问两个URL可以使用两个单独的按钮。

{
  "status": "ok",
  "status_message": "Query was successful",
  "data": {
    "movie_count": 6025,
    "limit": 20,
    "page_number": 1,
    "movies": [
      {
        "id": 6368,
        "imdb_code": "tt2364897",
        "title": "The Disappointments Room",
        "title_english": "The Disappointments Room",
        "title_long": "The Disappointments Room (2016)",
        "slug": "the-disappointments-room-2016",
        "year": 2016,
        "rating": 3.9,
        "runtime": 85,
        "genres": [
          "Drama",
          "Horror",
          "Thriller"
        ],
        "language": "English",
        "mpa_rating": "R",
        "background_image": "https://new.ps/assets/images/movies/the_disappointments_room_2016/background.jpg",
        "background_image_original": "https://new.ps/assets/images/movies/the_disappointments_room_2016/background.jpg",
        "small_cover_image": "https://new.ps/assets/images/movies/the_disappointments_room_2016/small-cover.jpg",
        "state": "ok",
        "torrents": [
          {
            "url": "https://new.ps/torrent/download/C9FED33A10E67EB46373CB8F5E6FA6FD6AFD91E8",
            "hash": "C9FED33A10E67EB46373CB8F5E6FA6FD6AFD91E8",
            "quality": "720p",
            "seeds": 800,
            "peers": 563,
            "size": "678.14 MB",
            "size_bytes": 711081329,
            "date_uploaded": "2017-03-06 16:03:23",
            "date_uploaded_unix": 1488834203
          },
          {
            "url": "https://new.ps/torrent/download/285CA3A886E8DE6FCF42D293A2404A8AD8F0CAC4",
            "hash": "285CA3A886E8DE6FCF42D293A2404A8AD8F0CAC4",
            "quality": "1080p",
            "seeds": 675,
            "peers": 462,
            "size": "1.4 GB",
            "size_bytes": 1503238554,
            "date_uploaded": "2017-03-06 17:36:49",
            "date_uploaded_unix": 1488839809
          }
        ],
        "date_uploaded": "2017-03-06 16:03:23",
        "date_uploaded_unix": 1488834203
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

假设您的json位于名为yourJsonObject的对象中,那么您可以在JS中循环遍历此数据:

yourJsonObject.data.movies.forEach(function(movie){
    movie.torrents.forEach(function(torrent){
        console.log("Torrent URL : " + torrent.url);
    });
});

如果您正在尝试使用ng-repeat指令进行查看。

<div ng-repeat="movie in yourJsonObject.data.movies">
    <div ng-repeat="torrent in movie.torrents">
        Torrent URL : {{torrent.url}} <br />
    </div>
</div>