解码嵌套JSON多“的”循环(Decoding nested JSON with multiple

2019-09-03 04:41发布

我是新来的Python(上周),并达到我的极限了。 花了三天时间就这样,我的大部分时间计算器,但我不知道如何再往前走!

所述的Json具有多个嵌套的数组。 它可以包含三个(如下面的例子(json.txt)一样),或30.我需要通过每一个回路,然后向下钻取以“局”,最后得到的“小门”的值。 这是最后一步,我很迷茫。 任何人都可以提出建议?

在彻底的绝望此致

import os, json,requests
print 'Starting'
url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt'

# download the json string
json_string = requests.get(url)
print 'Downloaded json'

# get the content
the_data = json_string.json()
print 'the_data has length ', len(the_data)
for index in range(len(the_data)):
    print 'Now working on index ', index
    for wicket in the_data[index]:
            print 'wicket equals ',wicket
                    # OK - I can see Innings. Now, how do I get inside
                    # and obtain 'wickets'?

Answer 1:

首先,不使用索引,但环直接在列表; 这样,你可以给他们有意义的名字。 顶层是条目列表,每个条目是一个字典'innings'键,每innings是字典列表,等等,一个wickets键:

for entry in data:
    for inning in entry['innings']:
        print inning['wickets']

这将打印:

>>> for entry in data:
...     for inning in entry['innings']:
...         print inning['wickets']
... 
10
9
0
0

这使得它更容易在每个级别也添加信息:

>>> for entry in data:
...     print entry['description']
...     for i, inning in enumerate(entry['innings']):
...         print 'Innings {}: {} wickets'.format(i + 1, inning['wickets'])
... 
Rest of Sri Lanka v Sri Lanka A at Pallekele, May 14, 2013
Innings 1: 10 wickets
Innings 2: 9 wickets
63rd match: Royal Challengers Bangalore v Kings XI Punjab at Bangalore, May 14, 2013
Innings 1: 0 wickets
Innings 2: 0 wickets
64th match: Chennai Super Kings v Delhi Daredevils at Chennai, May 14, 2013


Answer 2:

import os, json,requests
print 'Starting'
url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt'

# download the json string
json_string = requests.get(url)
print 'Downloaded json'

# get the content
the_data = json_string.json()
print 'the_data has length ', len(the_data)
for index in range(len(the_data)):
    print 'Now working on index ', index
    for d in the_data[index]['innings']:
        print d['wickets']


Answer 3:

它看起来丑陋,但你可以改进这一点,但在这里快译通和列表的搭配任意深度上市:

import os, json,requests
print 'Starting'
url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt'

# download the json string
json_string = requests.get(url)
print 'Downloaded json'

def dig_down(partial_json_list, depth):
    if type(partial_json_list) is list:
        for i in range(len(partial_json_list)):
            print 'index', i, ' at depth', depth,' has', len(partial_json_list[i]) , 'elements'
            if len(partial_json_list[i]) > 1:
                dig_down(partial_json_list[i],depth+1)
    else:
        for k in partial_json_list:
            print 'item at depth', depth, 'equals', k#, ' & has', len(partial_json_list[k]) , 'elements'
            if type(partial_json_list) is list or type(partial_json_list) is dict:
                try:
                    if len(partial_json_list[k]) > 1:
                        dig_down(partial_json_list[k],depth+1)
                except:
                    pass
            else:
                print partial_json_list[k]

# get the content
the_data = json_string.json()
print 'the_data has length ', len(the_data)
dig_down(the_data,0)


文章来源: Decoding nested JSON with multiple 'for' loops