我试图弄清楚如何在 Python 中查询 JSON 数组。有人可以告诉我如何通过一个相当复杂的数组进行简单的搜索和打印吗?
我使用的例子在这里:http ://eu.battle.net/api/wow/realm/status
例如,我想看看如何找到“Silvermoon”服务器,并打印出它的“人口”,然后是“Wintergrasp”数组中的“控制派系”。
数组片段当前如下所示:
{"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
这真的会帮助我掌握如何访问其他东西。