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