使用jQuery从Wikimedia解析json字符串 [英] parse json string from wikimedia using jquery
问题描述
正在尝试从Wiki页面获取信息框.为此,我正在使用Wiki API.以下是我从中获取json数据的网址.
Im tring to get the infobox from wiki pages. For this I'm using wiki api. The following is the url from which I'm getting json data.
http ://en.wikipedia.org/w/api.php?action = query& prop = revisions& rvprop = content& format = json& titles = "+ first +"& rvsection = 0
http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles="+first+"&rvsection=0
第一个是包含Wikipedia文章标题的变量.
Where first is a variable containing the article title for Wikipedia.
我发现解析此数据以制作出有意义的html极其复杂.
I'm finding it extremely complex to parse this data to make a meaningful html out of it.
我最初使用的是$.each
函数.但是循环很深,我不得不使用6到7次才能获得所需的实际数据.我认为会有比这更好的选择.
请帮助我.
I was usuing $.each
function initially. But the loop is very deep that I had to use 6-7 times to get to the actual data that I want. I think there would be better alternative than this.
Please help me.
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."}]}}}})
推荐答案
If you want the actual html as it is displayed in the wikipage, use action=parse instead. And yes, the result objects are deeply nested. But no reason to loop over them!
- 第一个属性始终是动作,在这里:
query
- 您已请求页面属性,因此您将收到
pages
- 通过页面ID进行键控.这是使用循环的唯一步骤
- 每个页面对象都有某些属性(例如标题),您对
revisions
感兴趣
- 这是一个修订对象数组,您需要唯一的第一个
- 修订版对象的sourcetext属性是
*
- the first property is always the action, here:
query
- you have requested properties of pages, so you will receive
pages
- which are keyed by their page id. This is the only step to use a loop
- Each page object has certain properties (like a title), you're interested in the
revisions
- this is an array of revision objects, you need the only and first
- the sourcetext property of a revision object is the
*
所以,就去做吧
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
不要忘记检查每个对象的存在!如果您不请求任何页面,则将没有页面对象.仅当页面数组"为空时才如此.页面可能缺少标题/标题无效或其他内容,因此没有任何修订.等
Dont forget to check for the existance of each object! If you requested no pages, there will be no pages object; this is only the case when the pages "array" is empty. A page may be missing/invalid title or something else, so that is has no revisions. etc.
这篇关于使用jQuery从Wikimedia解析json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!