我想从维基页面获取信息框。为此,我正在使用wiki api。以下是我从中获取json数据的URL。
http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles= “+第一+” &安培; rvsection = 0
首先是包含维基百科文章标题的变量。
我发现解析这些数据以创建一个有意义的html非常复杂。
我最初正在使用$.each
功能。但循环非常深,我不得不使用6-7次来获取我想要的实际数据。我认为会有更好的选择。
请帮帮我。
json数据供参考
jQuery16209061950308827726_1334683337112({"query":{"pages":{"11039790":{"pageid":11039790,"ns":0,"title":"Animal","revisions":[{"*":"{{Redirect|Animalia}}\n{{Other uses}}\n{{pp-semi-protected|small=yes}}\n{{pp-move-indef}}\n{{Taxobox\n| color = {{taxobox color|[[animalia]]}}\n| name = Animals\n| fossil_range = [[Ediacaran]] \u2013 Recent {{fossilrange|610|0|}}\n| image = Animal diversity.png\n| image_width = 250px\n| domain = [[Eukaryota]]\n{{Taxobox_norank_entry | taxon = [[Opisthokonta]]}}\n{{Taxobox_norank_entry | taxon = [[Holozoa]]}}\n{{Taxobox_norank_entry | taxon = [[Filozoa]]}}\n| regnum = '''Animalia'''\n| regnum_authority = [[Carolus Linnaeus|Linnaeus]], [[Systema Naturae|1758]]\n| subdivision_ranks = [[Phylum|Phyla]]\n| subdivision =\n* '''Subkingdom [[Parazoa]]'''\n** [[Sponge|Porifera]]\n** [[Placozoa]]\n* '''Subkingdom [[Eumetazoa]]'''\n** '''[[Radiata]] (unranked)'''\n*** [[Ctenophora]]\n*** [[Cnidaria]]\n** '''[[Bilateria]] (unranked)'''\n*** [[Orthonectida]]\n*** [[Rhombozoa]]\n*** [[Acoelomorpha]]\n*** [[Chaetognatha]]\n*** '''Superphylum [[Deuterostomia]]'''\n**** [[Chordata]]\n**** [[Hemichordata]]\n**** [[Echinoderm]]ata\n**** [[Xenoturbellida]]\n**** [[Vetulicolia]] [[extinction|\u2020]]\n*** '''[[Protostomia]] (unranked)'''\n**** '''Superphylum [[Ecdysozoa]]'''\n***** [[Kinorhyncha]]\n***** [[Loricifera]]\n***** [[Priapulida]]\n***** [[Nematoda]]\n***** [[Nematomorpha]]\n***** [[Lobopodia]]\n***** [[Onychophora]]\n***** [[Tardigrada]]\n***** [[Arthropoda]]\n**** '''Superphylum [[Platyzoa]]'''\n***** [[Platyhelminthes]]\n***** [[Gastrotricha]]\n***** [[Rotifera]]\n***** [[Acanthocephala]]\n***** [[Gnathostomulida]]\n***** [[Micrognathozoa]]\n***** [[Cycliophora]]\n**** '''Superphylum [[Lophotrochozoa]]'''\n***** [[Sipuncula]]\n***** [[Hyolitha]] [[extinction|\u2020]]\n***** [[Nemertea]]\n***** [[Phoronida]]\n***** [[Bryozoa]]\n***** [[Entoprocta]]\n***** [[Brachiopoda]]\n***** [[Mollusca]]\n***** [[Annelida]]\n***** [[Echiura]]\n}}\n\n'''Animals''' are a major group of multicellular, [[eukaryotic]] [[organism]]s of the [[Kingdom (biology)|kingdom]] '''Animalia''' or '''Metazoa'''. Their [[body plan]] eventually becomes fixed as they [[Developmental biology|develop]], although some undergo a process of [[metamorphosis]] later on in their life. Most animals are [[Motility|motile]], meaning they can move spontaneously and independently. All animals are also [[heterotroph]]s, meaning they must ingest other organisms or their products for [[sustenance]].\n\nMost known animal [[phylum|phyla]] appeared in the fossil record as marine species during the [[Cambrian explosion]], about 542 million years ago."}]}}}})
答案 0 :(得分:5)
如果您想要在wikipage中显示的实际html,请改用action=parse。 是的,结果对象是深层嵌套的。但没理由循环它们!
query
pages
revisions
*
所以,就这样做:
if (data && data.query && data.query.pages)
var pages = data.query.pages;
else
// error: No pages returned / other problems!
for (var id in pages) { // in your case a loop over one property
if (pages[id].revisions && pages[id].revisions[0] && pages[id].revisions[0]["*"])
var content = pages[id].revisions[0]["*"];
else
// error: No revision content returned for whatever reasons!
}
// use "content" variable here
别忘了检查每个物体的存在!如果您没有请求任何页面,则不会有页面对象;这只是“数组”页面为空时的情况。页面可能缺失/无效标题或其他内容,因此没有修订。等