在Python中遍历多维JSON数组

8
我正在尝试弄清如何在Python中查询JSON数组。请问有人能向我展示如何通过一个相当复杂的数组进行简单搜索和打印吗?
我使用的示例在此处:http://eu.battle.net/api/wow/realm/status 例如,我想查找'Silvermoon'服务器,并打印出其'population',然后是'Wintergrasp'数组中的'controlling-faction'。
目前,该数组片段如下:
{"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"}

目前我可以访问主数组,但似乎无法访问子数组,除非将整个数组复制到另一个新变量中,这似乎是浪费的。我希望能够做类似于下面的事情

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

这将帮助我更好地掌握如何访问其他内容。


1
刚才我还以为你在用Scala呢。 - Games Brainiac
3个回答

13
Python的交互模式是逐步探索结构化数据的好方法。很容易找到如何访问,比如说silvermoon服务器的数据:
>>> 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'

如果您还不了解它们,请阅读dictionary.keyslist.indexlist comprehensions以了解正在发生的事情。
在确定数据结构后,您最终可以重写数据访问以使其更易读和高效:
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是通过引用传递值by reference的。这意味着当您将某物赋值给新变量时,不涉及任何复制操作。以下是有关按值传递和按引用传递的简单解释

最后,您似乎过分担心性能问题。Python的哲学是先把它做对,再优化如果您需要更好的性能,请在确保正确性的前提下进行优化,如果这样做值得花费时间。


非常感谢,你逐步地讲解很有帮助。 - DA Morrison

1
这是您想要的:

这样做:

# -*- coding: utf-8 -*-
import json
import urllib2

def searchListOfDicts(listOfDicts, attr, value):
    """
    Loops through a list of dictionaries and returns matching attribute value pair
    You can also pass it slug, silvermoon or type, pve
    returns a list containing all matching dictionaries 
    """
    matches = [record for record in listOfDicts if attr in record and record[attr] == value]
    return matches

def myjsonDict():
    """
    Opens url, grabs json and puts it inside a dictionary
    """
    req = urllib2.Request("http://eu.battle.net/api/wow/realm/status", None, {})
    opener = urllib2.build_opener()
    f = opener.open(req)
    json_dict = json.load(f)
    return json_dict

jsonDict = myjsonDict()
#we want to search inside realms list
silverMoonServers = searchListOfDicts(jsonDict["realms"], "name", "Silvermoon")
#access first dictionary that matched "name, Silvermoon" query
print silverMoonServers[0]
print silverMoonServers[0]["wintergrasp"]
print silverMoonServers[0]["wintergrasp"]["controlling-faction"]

0
在Python中,json.loads将JSON对象映射到Python字典,将Arrays映射到list,因此进一步操作就像使用常规的Python dictlist结构一样。
以下是使用requestslamdbas的方法:
    import json
    import requests

    response = requests.get("http://eu.battle.net/api/wow/realm/status")
    json_data = json.loads(response.text)

    # loop through the list of realms to find the one you need (realm_name)
    get_realm = lambda realm_name, jd: [r for r in jd['realms'] 
                                        if r['name'] == realm_name]

    # extract data you need, if there is a match in the list of realms,
    # return None otherwise
    get_your_data = lambda realm: (
        realm[0]['name'],
        realm[0]['wintergrasp']['controlling-faction']
    ) if realm else None

    print get_your_data(get_realm('Silvermoon', json_data))

为什么在这么简单的事情上要引入对requests的依赖? - loopbackbee
为什么不呢?它是一个非常好的工具,值得了解和使用,即使是在这种情况下。它可以让简单的情况变得更加简单。 - Alexander Zhukov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接