小编典典

使用jquery从Wikimedia解析json字符串

json

试图从Wiki页面获取信息框。为此,我正在使用Wiki API。以下是我从中获取json数据的url。

http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=
“ + first +”&rvsection = 0

首先是一个变量,其中包含Wikipedia的文章标题。

我发现解析此数据以制作出有意义的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."}]}}}})

阅读 274

收藏
2020-07-27

共1个答案

小编典典

如果您想要实际的html页面上显示的html,请改用action =
parse
。是的,结果对象是深层嵌套的。但是没有理由在它们上面循环!

  • 第一个属性始终是动作,在这里: query
  • 您已请求页面属性,因此您将收到 pages
  • 并以其页面ID作为关键字。这是使用循环的唯一步骤
  • 每个页面对象都有某些属性(例如标题),您对 revisions
  • 这是一个修订对象数组,您需要唯一的一个
  • 修订对象的sourcetext属性是 *

因此,只需执行以下操作:

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

不要忘记检查每个对象的存在!如果您不请求任何页面,则将没有页面对象。仅当页面“数组”为空时才如此。页面可能缺少标题/标题无效或其他内容,因此该页面没有修订。等等

2020-07-27