成功解析这个深层嵌套的JSON列表

时间:2014-10-20 15:23:35

标签: python json list nested-lists

我有一个以下列格式返回的JSON对象:

[[[13,
   u'Arsenal',
   [[[[u'goal', u'openplay', u'header', [1]],
      [u'goal', u'openplay', u'leftfoot', [1]],
      [u'goal', u'openplay', u'rightfoot', [3]],
      [u'goal', u'owngoal', u'rightfoot', [1]],
      [u'miss', u'corner', u'header', [2]],
      [u'miss', u'corner', u'leftfoot', [3]],
      [u'miss', u'corner', u'rightfoot', [2]],
      [u'miss', u'crossedfreekick', u'rightfoot', [2]],
      [u'miss', u'directfreekick', u'leftfoot', [1]],
      [u'miss', u'openplay', u'header', [2]],
      [u'miss', u'openplay', u'leftfoot', [16]],
      [u'miss', u'openplay', u'rightfoot', [23]]]]]]]]

当我使用此代码时:

for match in responser: 
    for num_events, team, events in match:
        regex = {tuple(sub[:3]): sub[3][0] for y in events[0] for sub in y}

我将上面的嵌套列表作为字典返回,其中的键由从上述文本值派生的元组组成,其值也来自上述嵌套列表。

然后我有这个稍微不同的数据结构(再次是JSON):

[[[13,
   u'Arsenal',
   [[6.125,
     [[u'assist', u'cross', [3]],
      [u'normal', u'cross', [198]],
      [u'normal', u'longball', [326]],
      [u'assist', u'short', [5]],
      [u'normal', u'short', [4726]],
      [u'assist', u'throughball', [1]],
      [u'normal', u'throughball', [35]]]]]]]]

据我所知,这和第一组嵌套列表之间的唯一区别(除了每个列表有两个而不是三个文本元素)是值'6.125',它包含在一组之间嵌套括号。所以在我看来,下面的代码应该允许我解析第二个数据结构:

for match in responser: 
    for num_events, team, events in match:
        regex = {tuple(sub[:2]): sub[2][0] for y in events[0] for sub in y}

然而,这会返回以下错误:

exceptions.TypeError: 'float' object is not iterable

有谁能告诉我我的误解和纠正问题的语法?

0 个答案:

没有答案