Python-如何跳过特定的JSON元素? [英] Python - How to skip a specific JSON element?

查看:62
本文介绍了Python-如何跳过特定的JSON元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
"query": {
    "pages": {
        "7412236": {
            "pageid": 7412236,
            "ns": 0,
            "title": "Steve Jobs",
            "extract": "<p><b>Steven Paul</b> \"<b>Steve</b>\" <b>Jobs</b> (/\u02c8d\u0292\u0252bz/; February 24, 1955\u00a0\u2013 October 5, 2011) was an American entrepreneur, marketer, and inventor, who was the co-founder (along with Steve Wozniak and Ronald Wayne), chairman, and CEO of Apple Inc. Through Apple, he is widely recognized as a charismatic pioneer of the personal computer revolution and for his influential career in the computer and consumer electronics fields, transforming \"one industry after another, from computers and smartphones to music and movies\". Jobs also co-founded and served as chief executive of Pixar Animation Studios; he became a member of the board of directors of The Walt Disney Company in 2006, when Disney acquired Pixar. Jobs was among the first to see the commercial potential of Xerox PARC's mouse-driven graphical user interface, which led to the creation of the Apple Lisa and, one year later, the Macintosh. He also played a role in introducing the LaserWriter, one of the first widely available laser printers, to the market.</p>\n<p>After a power struggle with the board of directors in 1985, Jobs left Apple and founded NeXT, a computer platform development company specializing in the higher-education and business markets. In 1986, he acquired the computer graphics division of Lucasfilm, which was spun off as Pixar. He was credited in <i>Toy Story</i> (1995) as an executive producer. He served as CEO and majority shareholder until Disney's purchase of Pixar in 2006. In 1996, after Apple had failed to deliver its operating system, Copland, Gil Amelio turned to NeXT Computer, and the NeXTSTEP platform became the foundation for the Mac OS X. Jobs returned to Apple as an advisor, and took control of the company as an interim CEO. Jobs brought Apple from near bankruptcy to profitability by 1998.</p>\n<p>As the new CEO of the company, Jobs oversaw the development of the iMac, iTunes, iPod, iPhone, and iPad, and on the services side, the company's Apple Retail Stores, iTunes Store and the App Store. The success of these products and services provided several years of stable financial returns, and propelled Apple to become the world's most valuable publicly traded company in 2011. The reinvigoration of the company is regarded by many commentators as one of the greatest turnarounds in business history.</p>\n<p>In 2003, Jobs was diagnosed with a pancreas neuroendocrine tumor. Though it was initially treated, he reported a hormone imbalance, underwent a liver transplant in 2009, and appeared progressively thinner as his health declined. On medical leave for most of 2011, Jobs resigned in August that year, and was elected Chairman of the Board. He died of respiratory arrest related to his tumor on October 5, 2011.</p>\n<p>Jobs received a number of honors and public recognition for his influence in the technology and music industries. He has been referred to as \"legendary\", a \"futurist\" or simply \"visionary\", and has been described as the \"Father of the Digital Revolution\", a \"master of innovation\", \"the master evangelist of the digital age\" and a \"design perfectionist\".</p>\n<p></p>"
        }
    }
}
}

因此,我使用以下代码以JSON格式获取Wikipedia API提供的内容,

So I fetch content provided by Wikipedia API in JSON format using the following code,

fetchedPage = urllib2.urlopen('https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro&titles=Steve%20Jobs&format=json')
Json = json.load(fetchedPage)
content = Json['query']['pages']['7412236']['extract']
print content

它可以很好地用于单篇文章,因为我可以手动输入文章的"pageid".但从更一般的角度来看,我将不得不跳过"pageid"元素,以便直接获取任何文章的内容.

It works fine for a single article as I can manually enter the article's "pageid". But on a more general basis I'll have to skip the "pageid" element so that I can fetch the content of any article directly.

简而言之,我想实现这样的目标

In short, I want to achieve something like this,

content = Json['query']['pages'][//I dont care what's in this element]['extract']

我该如何进行?

推荐答案

您可以通过

You can access to the first element of a dictionary through dict.itervalues().next() without the key. The itervalues() returns a iterable of values in the dictionnary, and the next() returns the first element of this iterable.

fetchedPage = urllib2.urlopen('https://en.wikipedia.org/w/api.php?action=query&
prop=extracts&exintro&titles=Steve%20Jobs&format=json')
Json = json.load(fetchedPage)
content = Json['query']['pages'].itervalues().next()['extract']
print content

如果您觉得更明确,则可以使用dict[dict.keys()[0]]获取dict的第一把键.在您的示例中,您将拥有:

Another way can be to get the first key of the dict with dict[dict.keys()[0]] if you feel it more explicit. In your example you'll have:

page_id = Json['query']['pages'].keys()[0]
content = Json['query']['pages'][page_id]['extract']

这篇关于Python-如何跳过特定的JSON元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆