我正在尝试访问"描述"在这个url中的json中可用:https://librivox.org/api/feed/audiobooks/id/11591/extended/1/format/json在PhantomJS脚本中。我的脚本只有一步:
steps = [
/*
* Step 1 - print out description
*/
function(){
page.open( 'https://librivox.org/api/feed/audiobooks/id/11591/extended/1/format/json' , function () {
var jsonSource = page.plainText;
//console.log( jsonSource );
var resultObject = JSON.parse(jsonSource);
var bookkey = Object.keys(resultObject.books);
console.log( bookkey );
console.log( resultObject.books.bookkey );
});
},
];
当我运行我的脚本时,我得到了这个输出:
14
undefined
我不确定如何访问描述值。
以下是来自上述网址的json:
{"books":{"14":{"id":"11591","title":"Soldier Of The Legion; <br>An Englishman's Adventures Under The French Flag in Algeria And Tonquin","description":"An educated gentleman, Mr Manington has given an insight into the unusual experiences of an Englishman in the French Foreign Legion, such as no ordinary \"mercenary\" could have done. Most of the narrative deals with Tonquin, and the fighting there against the rebels in their forest fastnesses. Incidentally, in giving an account of his friendship for the native sergeant, Doy-Tho, the author has been able to impart to the pages of the book an Oriental atmosphere that we think will prove attractive to the reader. - Summary by Editors' Note","url_text_source":"http:\/\/www.gutenberg.org\/ebooks\/53902","language":"English","copyright_year":"1907","num_sections":"15","url_rss":"http:\/\/librivox.org\/rss\/11591","url_zip_file":"http:\/\/www.archive.org\/download\/\/a_soldier_of_the_legion_1702_librivox\/a_soldier_of_the_legion_1702_librivox_64kb_mp3.zip","url_project":"","url_librivox":"http:\/\/librivox.org\/a-soldier-of-the-legion-an-englishmans-adventures-under-the-french-flag-in-algeria-and-tonquin-by-george-manington\/","url_other":null,"totaltime":"07:56:47","totaltimesecs":28607,"authors":[{"id":"12503","first_name":"George","last_name":"Manington","dob":"1866","dod":"?"}],"url_iarchive":"http:\/\/archive.org\/details\/a_soldier_of_the_legion_1702_librivox","sections":[{"id":"392478","section_number":"0","title":"Dedication, Preface, Editors' Note","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_00_manington_128kb.mp3","language":"English","playtime":"316","file_name":"soldierlegion_00_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392479","section_number":"1","title":"Chapter 1 Ministere de la Guerre, The Recruiting Office, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_01_manington_128kb.mp3","language":"English","playtime":"3049","file_name":"soldierlegion_01_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392480","section_number":"2","title":"Chapter 2 General Inspection, The Band Of The Legion, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_02_manington_128kb.mp3","language":"English","playtime":"1923","file_name":"soldierlegion_02_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392481","section_number":"3","title":"Chapter 3 Part 1 Some Information Concerning Tonquin, Haiphong, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_03_manington_128kb.mp3","language":"English","playtime":"2561","file_name":"soldierlegion_03_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392819","section_number":"4","title":"Chapter 3 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_04_manington_128kb.mp3","language":"English","playtime":"2495","file_name":"soldierlegion_04_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392482","section_number":"5","title":"Chapter 4 Part 1 The Difficulties Of Obtaining Military Intelligence, Native spies, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_05_manington_128kb.mp3","language":"English","playtime":"2171","file_name":"soldierlegion_05_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"394973","section_number":"6","title":"Chapter 4 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_06_manington_128kb.mp3","language":"English","playtime":"2374","file_name":"soldierlegion_06_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392483","section_number":"7","title":"Chapter 5 Part 1 An execution, A Rebel Chieftan, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_07_manington_128kb.mp3","language":"English","playtime":"1860","file_name":"soldierlegion_07_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"395106","section_number":"8","title":"Chapter 5 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_08_manington_128kb.mp3","language":"English","playtime":"1835","file_name":"soldierlegion_08_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392484","section_number":"9","title":"Chapter 6 Part 1 La Soeur Agnes, Exeat, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_09_manington_128kb.mp3","language":"English","playtime":"2108","file_name":"soldierlegion_09_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"395107","section_number":"10","title":"Chapter 6 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_10__manington_128kb.mp3","language":"English","playtime":"2007","file_name":"soldierlegion_10__manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392485","section_number":"11","title":"Chapter 7 Part 1 The Last Struggles Of A Rebellion, Departure Of Captain Plessier, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_11_manington_128kb.mp3","language":"English","playtime":"1611","file_name":"soldierlegion_11_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"395159","section_number":"12","title":"Chapter 7 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_12_maningon_128kb.mp3","language":"English","playtime":"1306","file_name":"soldierlegion_12_maningon_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"392507","section_number":"13","title":"Chapter 8 Part 1 General Voyron, Organisation Of The Brigade, etc","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_13_manington_128kb.mp3","language":"English","playtime":"1673","file_name":"soldierlegion_13_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]},{"id":"395160","section_number":"14","title":"Chapter 8 Part 2","listen_url":"https:\/\/librivox.org\/uploads\/aradlaw\/soldierlegion_14_manington_128kb.mp3","language":"English","playtime":"1318","file_name":"soldierlegion_14_manington_128kb.mp3","readers":[{"reader_id":"6454","display_name":"David Wales"}]}],"genres":[{"id":"73","name":"War & Military"},{"id":"108","name":"Travel & Geography"}],"translators":[]}}}
答案 0 :(得分:2)
从评论转到答案。
尝试:resultObject.books[bookkey[0]]
这应该允许您拨打resultObject.books[bookkey[0]].description
。
享受。
注意强>
参见示例:https://jsfiddle.net/Twisty/fL129dnz/&amp; https://jsfiddle.net/Twisty/fL129dnz/3/
Objects.keys()
返回一组键。似乎某些浏览器可能接受包含单个元素作为索引的数组。对于这种情况,由于只调出了一本书,因此可以使用:resultObject.books[bookkey]
。为了更加便携,我建议谨慎并定义一个结果:resultObject.books[bookkey[0]]
感谢@torazaburo指出这一点。