如何将评级数组抓取到json中
Posted
技术标签:
【中文标题】如何将评级数组抓取到json中【英文标题】:How to grab ratings array into in json 【发布时间】:2018-08-11 08:58:36 【问题描述】:您好 *** 用户:
我正在调整一个使用 omdb api 来搜索电影的网站,而且我对使用 json 和 Ajax 不熟悉。该网站使用淘汰赛js框架。我的问题是:你如何设置一个可淘汰的可观察命令行来收集一个看起来像一个信息数组的 json 设置。我正在尝试获取此 json 的 Ratings 部分(该部分以 bold 突出显示)
"Title": "Batman Begins",
"Year": "2005",
"Rated": "PG-13",
"Released": "15 Jun 2005",
"Runtime": "140 min",
"Genre": "Action, Adventure, Thriller",
"Director": "Christopher Nolan",
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
"Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
"Language": "English, Urdu, Mandarin",
"Country": "USA, UK",
"Awards": "Nominated for 1 Oscar. Another 14 wins & 71 nominations.",
"Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BYzc4ODgyZmYtMGFkZC00NGQyLWJiMDItMmFmNjJiZjcxYzVmXkEyXkFqcGdeQXVyNDYyMDk5MTU@._V1_SX300.jpg",
**"Ratings": [
"Source": "Internet Movie Database",
"Value": "8.3/10"
,
"Source": "Rotten Tomatoes",
"Value": "84%"
,
"Source": "Metacritic",
"Value": "70/100"
],**
"Metascore": "70",
"imdbRating": "8.3",
"imdbVotes": "1,099,197",
"imdbID": "tt0372784",
"Type": "movie",
"DVD": "18 Oct 2005",
"BoxOffice": "$204,100,000",
"Production": "Warner Bros. Pictures",
"Website": "http://www.batmanbegins.com/",
"Response": "True"
这些是用于抓取数据的 ko 设置:
self.currentMovie =
Type: ko.observable(),
Year: ko.observable(),
Genre: ko.observable(),
Released: ko.observable(),
Runtime: ko.observable(),
Poster: ko.observable(),
Rated: ko.observable(),
imdbRating: ko.observable(),
imdbVotes: ko.observable(),
Ratings:ko.observable().Source = "Rotten Tomatoes",
Actors: ko.observable(),
Plot: ko.observable(),
Writer: ko.observable(),
Director: ko.observable(),
Country: ko.observable(),
Language: ko.observable(),
Title: ko.observable()
;
我的第一个冲动是像这样设置 observable:
Ratings:ko.observable().Source = "Rotten Tomatoes",
但所做的只是逐字列出信息,而不是其余信息。我应该使用 ko.observableArray 还是可以使用其他设置?
【问题讨论】:
试图获得最大评分来源? 基本上。基本上,我正在尝试获取 json 中可用的烂番茄值。 检查答案,如果您正在寻找相同的东西,请告诉我 我在 self.currentMovie 之后尝试了原始行,但我从行中得到了一些错误;一个声明变量“o”是隐式声明的,并且在 => 所在的行处预期表达式。我试图“像这样稍微调整一下这条线:self.currentMovie.Ratings = Ratings.find(o => o.Source === 'Rotten Tomatoes'); 但它也没有用。谢谢你帮助我. 这东西对我来说很奇怪而且很新鲜。 那么在做出改变之后它起作用了吗? 【参考方案1】:首先,如果 API 以数组的形式提供信息,您应该使用 Observable 数组。接下来,您说您要过滤数组以仅获取具有 Rotten Tomatoes 分数的对象,并将“值”作为可观察对象访问。对吗?
Knockout 提供了实用函数(阅读更多 here),包括一个用于过滤数组的函数,称为 ko.utils.arrayFilter
。所以你可以这样做:
self.currentMovie =
....
RatingsArray: ko.observableArray(),
....
var ratingFilterFunction = function (obArray)
var trimmedArray = ko.utils.arrayFilter(obArray(), function (item)
return item["Source"] === 'Rotten Tomatoes';
);
return ko.observable(trimmedArray[0]["Value"]);
;
self.CurrentMovie.Ratings = ratingFilterFunction(self.CurrentMovie.RatingsArray);
【讨论】:
'plus 1' ..好一个我完全忘记了淘汰赛的实用功能【参考方案2】:function vm()
var self = this;
var allList =
"Title": "Batman Begins",
"Year": "2005",
"Rated": "PG-13",
"Released": "15 Jun 2005",
"Runtime": "140 min",
"Genre": "Action, Adventure, Thriller",
"Director": "Christopher Nolan",
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
"Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
"Language": "English, Urdu, Mandarin",
"Country": "USA, UK",
"Awards": "Nominated for 1 Oscar. Another 14 wins & 71 nominations.",
"Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BYzc4ODgyZmYtMGFkZC00NGQyLWJiMDItMmFmNjJiZjcxYzVmXkEyXkFqcGdeQXVyNDYyMDk5MTU@._V1_SX300.jpg",
"Ratings": [
"Source": "Internet Movie Database",
"Value": "8.3/10"
,
"Source": "Rotten Tomatoes",
"Value": "84%"
,
"Source": "Metacritic",
"Value": "70/100"
],
"Metascore": "70",
"imdbRating": "8.3",
"imdbVotes": "1,099,197",
"imdbID": "tt0372784",
"Type": "movie",
"DVD": "18 Oct 2005",
"BoxOffice": "$204,100,000",
"Production": "Warner Bros. Pictures",
"Website": "http://www.batmanbegins.com/",
"Response": "True"
self.Rating = ko.observable(allList.Ratings.find(o => o.Source === 'Rotten Tomatoes'));
// Activates knockout.js
ko.applyBindings(new vm());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<!-- This is a *view* - html markup that defines the appearance of your UI -->
<div data-bind="text: Rating().Value">
</div>
假设您通过对 allList 对象的 ajax 调用收到了 JSON。您可以通过allList.Ratings
获取您的 Ratings 数组,您可以在其中获取您正在寻找的来源。
你可能会这样做
self.Rating = ko.observable(allList.Ratings.find(o => o.Source === 'Rotten Tomatoes'));
假设您在 moviesRatingJson 对象中有所需的 Ratings JSON
【讨论】:
仍然没有骰子。关于 self.Rating = ko.observable(allList.Ratings.find(o => o.Source === '烂番茄'));行,它给出一个错误,指出该对象是在命令行的 o => o.Source 部分隐式声明的。但是您的解决方案有效。我想知道:Ray 表示 omdb 将 json 信息作为数组提供。我应该添加 allList var,然后用 ko.obeservableArray 行转储 ko.obeservable 行吗? 试一试。至少您的示例 Json 不是数组。以上是关于如何将评级数组抓取到json中的主要内容,如果未能解决你的问题,请参考以下文章
Android -- 如何从应用程序内将应用程序评级/评论发布到市场?