如何将 JSON 数据项转换为变量

how to convert json data item into variable?

本文关键字:转换 变量 数据项 JSON      更新时间:2023-09-26

我正在处理来自电视 API 的 Json 数据。从这个 api 中,我想使用"tvshow.episode.ratings.loved"值。如何将此值链接到类似$scope.喜欢=电视节目.剧集.收视率.喜欢;我想用这个做一些数学运算。如何将这个冗长的 JSON 文本转换为 $scope.like?我应该在代码中的什么位置包含它?请帮助

var app = angular.module('MyApp',[]);
app.controller("mainController", function ($scope, $http, $filter){
    $scope.filteredItems = [];       
    $scope.pagedItems = [];
    $scope.curPage = 1;
     $scope.init = function() {
        $http.jsonp('http://api.trakt.tv/calendar/premieres.json/' + $scope.apiKey + '/' + apiDate + '/' + 30 + '/?callback=JSON_CALLBACK').success(function(data) {
            angular.forEach(data, function(value, index){

                angular.forEach(value.episodes, function(tvshow, index){
                    tvshow.date = date; 
                    $scope.results.push(tvshow);
                    angular.forEach(tvshow.show.genres, function(genre, index){
                        //Only add to the availableGenres array if it doesn't already exist
                        var exists = false;
                        angular.forEach($scope.availableGenres, function(avGenre, index){
                            if (avGenre == genre) {
                                exists = true;
                            }
                        });
                    });
                });
            });                  
            };  

        }).error(function(error) {
        });
    };
    app.$inject = ($scope, $filter);

});

之后

$scope.groupedItems = [];

$scope.loved = [];

并在此循环中添加

 angular.forEach(value.episodes, function(tvshow, index) {
      // .... code
    $scope.loved.push(tvshow.ratings.loved);
})

如果您只想显示每个项目的评级,则需要添加模板

<span class="label"><i class="icon-thumbs-up"></i> {{tvshow.show.ratings.loved}}</span> 
<span class="label"><i class="icon-thumbs-down"></i> {{tvshow.show.ratings.hated}}</span> 

结果,您将获得所有"喜爱评级"的数组