如何将 JSON 数据项转换为变量
how to convert json data item into variable?
我正在处理来自电视 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>
结果,您将获得所有"喜爱评级"的数组
相关文章:
- 如何在 Javascript 中将变量的值从字符串转换为整数
- 将地理编码结果转换为php变量以发布到mysql数据库
- 如何在html中将字符串转换为数字?以及如何将变量传递到scriptlet代码中
- 将标量变量转换为javascript中包含变量的数组
- 使用Javascript将多行变量转换为单行变量
- 如何将JSON数据转换为变量,解析并输出为HTML
- 在Javascript中将条件语句转换为变量
- JavaScript将字符串转换为变量名
- 变量转换为另一个“未定义”的变量
- 将angular变量{{$index+1}}转换为javascript整数
- 如何在javascript中将String变量转换为int
- 为什么 Javascript 在变量赋值时将 null 转换为字符串
- 使用 JSON.parse 将字符串(变量)转换为对象,错误意外标记
- 提交时将JavaScript变量转换为PHP表单
- 如何在basil.js中将.txt字符串转换为变量
- 从外部javascript文件读取var并将其转换为php变量
- Javascript将字符串转换为变量递归
- 如何将HTML中的值转换为PHP变量
- 在d3.js中将局部变量转换为全局变量
- 每个部分附加一次多个链接转换变量