2013-05-14 45 views
1

我是Python新手(上週),已經達到了我的極限。花了三天的時間,大部分時間都在stackoverflow上,但我無法解決如何進一步發展!用多個'for'循環解碼嵌套的JSON

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'? 
+0

你是說,嵌套的深度可能是3或30,或該號碼的陣列(它們都是相同的深度)可能是3或30?如果您發佈了一些您試圖解析的json示例,可能會有所幫助。 – 2013-05-14 17:24:48

+0

@MarkkuK:代碼中的收件箱URL實際上是實時的。不是提供樣本的最佳方式,但這就是我能夠構建答案的方式。 – 2013-05-14 17:27:31

回答

4

首先,不要用在列表的索引,但環直接;這樣你可以給他們有意義的名字。頂層是條目列表,每個條目是一個'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 
+0

Martijn - 非常有幫助。非常感謝你!這對我的理解來說是一個巨大的進步;我還不瞭解第二部分。例如,「打印」Innings {}:{} wickets'.format(i + 1,inning ['wickets'])「 - {}括號。它將如何包括第一局和第二個門票?在我看來,這很多都像巫師魔法! – Will 2013-05-14 20:19:54

+0

@Will:我所做的只是添加2個'print'語句; ['enumerate()'函數](http://docs.python.org/2/library/functions.html#enumerate)爲循環添加了一個計數器,['str.format()'方法]( http://docs.python.org/2/library/stdtypes.html#str.format)只是將一些數據放入字符串的一種方法。 – 2013-05-14 20:23:05

+0

@Will:您可以爲'{}'佔位符'添加數字''Innings {0}:{1} wickets'',但是如果您省略它們,那麼Python會自動爲您「計算」它們。 – 2013-05-14 20:23:59

0
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'] 
0

它看起來醜陋,但你可以改進這一點,但在這裏快譯通和列表的搭配任意深度上市:

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)