Python - 如何跳过特定的 JSON 元素?

标签 python json python-2.7

{
"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 提供的内容,

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”元素,以便我可以直接获取任何文章的内容。

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

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

我该如何继续?

最佳答案

您可以通过dict.itervalues().next() 访问字典的第一个元素。没有 key 。 itervalues() 返回字典中值的可迭代对象,next() 返回此可迭代对象的第一个元素。

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]] 获取字典的第一个键。在您的示例中,您将拥有:

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

关于Python - 如何跳过特定的 JSON 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20801197/

相关文章:

javascript - 如何使用 Node js 将 json 发送到我的 View

带有 json 收集数据的 PHP 页面在 ubuntu 上不起作用,在 Windows 服务器上也能同样工作

python - 向字符串填充空格或零

algorithm - 提高模块效率

multithreading - pyQt和线程应用程序崩溃

python - 多类分类的每类 F1 分数

python - 检查 pandas 中的 'None' 值时排除 'NaN'

python - 我应该让长轮询连接保持多长时间?

python - WTForms:安装 'email_validator' 以支持电子邮件验证

c++ - boost json 解析器的 json 数组映射问题