小编DA *_*son的帖子

在Python中导航多维JSON数组

我试图弄清楚如何在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"}
Run Code Online (Sandbox Code Playgroud)

目前我可以访问主数组,但似乎无法访问子数组而不将整个事件复制到另一个看起来很浪费的新变量.我希望能够做类似的事情

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
Run Code Online (Sandbox Code Playgroud)

这真的可以帮助我掌握如何访问其他东西.

python json

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

json ×1

python ×1