gpt4 book ai didi

javascript - 如何访问动态键下的json数据项?

转载 作者:行者123 更新时间:2023-11-30 15:27:13 25 4
gpt4 key购买 nike

我正在尝试访问此 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

我不确定我应该如何访问描述值。

这是来自上述 URL 的 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":[]}}}

最佳答案

从评论到回答。

尝试:resultObject.books[bookkey[0]]

这应该允许您调用 resultObject.books[bookkey[0]].description

享受吧。

注意

参见示例:https://jsfiddle.net/Twisty/fL129dnz/ & https://jsfiddle.net/Twisty/fL129dnz/3/

Objects.keys() 返回键数组。似乎某些浏览器可能接受包含单个元素的数组作为索引。对于这种情况,由于只调用了一本书,因此可以使用:resultObject.books[bookkey]。为了更便携,我建议谨慎并定义单个结果:resultObject.books[bookkey[0]]

感谢@torazaburo 指出这一点。

关于javascript - 如何访问动态键下的json数据项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42774266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com