在Python中浏览多维JSON数组 [英] Navigating Multi-Dimensional JSON arrays in Python

查看:120
本文介绍了在Python中浏览多维JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何在Python中查询JSON数组.有人可以告诉我如何进行简单的搜索并通过相当复杂的数组进行打印吗?

I'm trying to figure out how to query a JSON array in Python. Could someone show me how to do a simple search and print through a fairly complex array please?

我使用的示例在这里: http://eu.battle.net/api/wow/境界/状态

The example I'm using is here: http://eu.battle.net/api/wow/realm/status

例如,我想看看如何找到"Silvermoon"服务器,并打印出其人口",然后打印出"Wintergrasp"阵列中的控制派".

I'd like to see, for example, how to find the 'Silvermoon' server, and print say its 'population', then the 'controlling-faction' within the 'Wintergrasp' array.

数组片段当前如下所示:

The array snippet currently looks like this:

{"type":"pve","population":"high","queue":false,"wintergrasp":{"area":1,"controlling-faction":0,"status":0,"next":1382350068792},"tol-barad":{"area":21,"controlling-faction":0,"status":0,"next":1382349141932},"status":true,"name":"Silvermoon","slug":"silvermoon","battlegroup":"Cyclone / Wirbelsturm","locale":"en_GB","timezone":"Europe/Paris"}

目前,我可以访问主数组,但是如果不将整个内容复制到另一个看起来很浪费的新变量中,似乎无法访问子数组.我希望能够做

At the moment I can access the main array, but don't seem to be able to access sub-arrays without copying the whole thing to another new variable which seems wasteful. I'd like to be able to do something like

import urllib2
import json

req = urllib2.Request("http://eu.battle.net/api/wow/realm/status", None, {})
opener = urllib2.build_opener()
f = opener.open(req)

x = json.load(f)  # open the file and read into a variable

# search and find the Silvermoon server
silvermoon_array = ????

# print the population
print silvermoon_array.????

# access the Wintergrasp sub-array
wintergrasp_sub = ????
print wintergrasp_sub.????  # print the controlling-faction variable

这确实可以帮助我掌握如何访问其他内容.

This would really help me get to grips with how to access other things.

推荐答案

Python的互动模式是逐步探索结构化数据的好方法.例如,很容易找到如何访问Silvermoon服务器数据:

Python's interactive mode is a great way to progressively explore structured data. It's easy to find how to access, say, the silvermoon server data:

>>> data=json.load(urllib2.urlopen("http://eu.battle.net/api/wow/realm/status"))
>>> type(data)
<type 'dict'>
>>> data.keys()
[u'realms']
>>> type(data['realms'])
<type 'list'>
>>> type(data['realms'][0])
<type 'dict'>
>>> data['realms'][0].keys()
[u'status', u'wintergrasp', u'battlegroup', u'name', u'tol-barad', u'locale', u'queue', u'timezone', u'type', u'slug', u'population']
>>> data['realms'][0]['name']
u'Aegwynn'
>>> [realm['name'] for realm in data['realms']].index('Silvermoon')
212
>>> silvermoon= data['realms'][212]
>>> silvermoon['population']
u'high'
>>> type(silvermoon['wintergrasp'])
<type 'dict'>
>>> silvermoon['wintergrasp'].keys()
[u'status', u'next', u'controlling-faction', u'area']
>>> silvermoon['wintergrasp']['controlling-faction']
>>> silvermoon['population']
u'high'

如果您还不了解它们,则应该阅读词典.keys list.index 列表理解以了解发生了什么.

If you don't know about them yet, you should read up on dictionary.keys, list.index and list comprehensions to understand what's going on.

弄清楚数据的结构之后,您最终可以重写数据访问,以使其更具可读性和效率:

After figuring out the structure of the data, you can finally rewrite the data access to be a bit more readable and efficient:

realms= data['realms']
realm_from_name= dict( [(realm['name'], realm) for realm in realms])
print realm_from_name['Silvermoon']['population']
print realm_from_name['Silvermoon']['wintergrasp']['controlling-faction']

关于将数组复制到另一个浪费的变量,您应该知道python传递值通过引用.这意味着在为新变量分配内容时不涉及复制. 这是按值传递与按引用传递的简单解释

As to copying the array to another variable being wasteful, you should know that python passes value by reference. That means that there's no copying involved when you assign something to a new variable. Here's a simple explanation of passing by value vs passing by reference

最后,您似乎对性能过于担心. Python的理念是先正确处理,然后再优化. 正常运行时,并且如果需要更好的性能,请优化它,如果值得的话.

Finally, you seem to be excessively worried with performance. Python's philosophy is get it right first, optimize later. When you have this working correctly and if you need better performance, optimize it, if it's worth the time.

这篇关于在Python中浏览多维JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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