2017-01-02 133 views
0

檢索提取所以我得到一個JSON響應,看起來像這樣:維基百科API

{ 
"batchcomplete": "", 
"query": { 
    "pages": { 
     "97646": { 
      "pageid": 97646, 
      "ns": 0, 
      "title": "Die Hard", 
      "extract": "Die Hard is a 1988 American action film directed by John McTiernan and written by Steven E. de Souza and Jeb Stuart. It follows off-duty New York City Police Department officer John McClane (Bruce Willis) as he takes on a group of highly organized criminals led by Hans Gruber (Alan Rickman), who perform a heist in a Los Angeles skyscraper under the guise of a terrorist attack using hostages, including McClane's wife Holly (Bonnie Bedelia), to keep the police at bay.\nIt is based on Roderick Thorp's 1979 novel Nothing Lasts Forever, the sequel to 1966's The Detective, which was adapted into a 1968 film of the same name that starred Frank Sinatra. Fox was therefore contractually obligated to offer Sinatra the lead role in Die Hard, but he turned it down. The studio then pitched the film to Arnold Schwarzenegger as a sequel to his 1985 action film Commando; he turned it down, as well, and the studio finally and reluctantly gave it to Willis, then known primarily as a comedic television actor.\nMade for $28 million, Die Hard grossed over $140 million theatrically worldwide, and was given a positive reception from critics. The film turned Willis into an action star, became a metonym for an action film in which a lone hero fights overwhelming odds, and has been named one of the best action movies ever made. The film also ranks #29 on Empire magazine's 2008 list of the 500 greatest movies of all time. The film's success spawned the Die Hard franchise, which includes four sequels (Die Hard 2, Die Hard with a Vengeance, Live Free or Die Hard and A Good Day to Die Hard), video games, and a comic book." 
     } 
    } 
} 
} 

這裏是鏈接:https://en.wikipedia.org/w/api.php?format=jsonfm&action=query&prop=extracts&exintro=&explaintext=&titles=Die%20Hard

我只想獲得頁面的提取物。麻煩的是我不知道pageid,直到我得到響應,所以不能調用提取。

要獲取這個例子中,提取物我必須做的:

var data = JSON.parse(this.response); 
console.log(suggestionData.query.pages.97646.extract); 

所以我試圖通過使一個變量加入到這樣的查詢來獲取頁面ID的值:

var data = JSON.parse(this.response); 
var pageid = [suggestionData.query.pages[0]; 
console.log(suggestionData.query.pages. + pageid +.extract); 

但作爲變量的pageid上來,因爲這一直沒有successeful:

"97646": { 
      "pageid": 97646, 
      "ns": 0, 
      "title": "Die Hard", 
      "extract": "Die Hard is a 1988 American action film directed by John McTiernan and written by Steven E. de Souza and Jeb Stuart. It follows off-duty New York City Police Department officer John McClane (Bruce Willis) as he takes on a group of highly organized criminals led by Hans Gruber (Alan Rickman), who perform a heist in a Los Angeles skyscraper under the guise of a terrorist attack using hostages, including McClane's wife Holly (Bonnie Bedelia), to keep the police at bay.\nIt is based on Roderick Thorp's 1979 novel Nothing Lasts Forever, the sequel to 1966's The Detective, which was adapted into a 1968 film of the same name that starred Frank Sinatra. Fox was therefore contractually obligated to offer Sinatra the lead role in Die Hard, but he turned it down. The studio then pitched the film to Arnold Schwarzenegger as a sequel to his 1985 action film Commando; he turned it down, as well, and the studio finally and reluctantly gave it to Willis, then known primarily as a comedic television actor.\nMade for $28 million, Die Hard grossed over $140 million theatrically worldwide, and was given a positive reception from critics. The film turned Willis into an action star, became a metonym for an action film in which a lone hero fights overwhelming odds, and has been named one of the best action movies ever made. The film also ranks #29 on Empire magazine's 2008 list of the 500 greatest movies of all time. The film's success spawned the Die Hard franchise, which includes four sequels (Die Hard 2, Die Hard with a Vengeance, Live Free or Die Hard and A Good Day to Die Hard), video games, and a comic book." 
} 

我也要使用JavaScript和jQuery的不是等

回答

1

試試這個小提琴:https://jsfiddle.net/u9dnm70b/

var response = '{"batchcomplete": "","query": {"pages": {"97646": {"extract": "abcd" }}}}'; 
var suggestionData = JSON.parse(response); 
var pageid = Object.keys(suggestionData.query.pages)[0]; 
console.log(suggestionData.query.pages[pageid].extract); 
1

你可以這樣提取的pageid:

var pageid = Object.keys(suggestionData.query.pages)[0]; 

那麼這應該工作:

console.log(suggestionData.query.pages[pageid].extract"); 
+0

真棒,唯一的是我得到一個' Uncaught SyntaxError:Unexpected token +'from your second line you've suggested? – Jess

+1

對不起,我的代碼出錯了。我剛剛複製了你的代碼片段,但這不正確。 –

+1

是的,它不知道如何格式化它 - 看起來應該是: 'console.log(suggestionData.query.pages [pageid] .extract);' – Jess

1

您可以使用Object.keys(jsonObj);找出鑰匙。 在你的情況下,var pageid是一個json對象。要訪問值,看看這個線程(get keys of json-object in JavaScript

在簡短的回答是

for key in pageid 
    content= pageid[key] 
    extract = content.extract