【发布时间】:2018-08-11 08:58:36
【问题描述】:
您好 StackOverflow 用户:
我正在调整一个使用 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'); 但它也没有用。谢谢你帮助我. 这东西对我来说很奇怪而且很新鲜。
-
那么在做出改变之后它起作用了吗?
标签: json knockout.js