用多个'for'循环解码嵌套的JSON [英] Decoding nested JSON with multiple 'for' loops

查看:200
本文介绍了用多个'for'循环解码嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python新手(上周),已经达到了我的极限。花了三天的时间,我大部分时间都在stackoverflow,但是我不能解决问题。

Json 有多个嵌套数组。它可以包含三个(如下面的例子(json.txt)),或30.我需要遍历每一个,然后深入到局,最后得到小门的价值。这是我迷惑的最后一步。任何人都可以提供建议吗?

你完全绝望吗?



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

#下载json字符串
json_string = requests.get(url)
print'已下载的json'

#获取内容
the_data = json_string.json()
print'the_data has length',len(the_data)
用于范围内的索引(len(the_data)):
print'正在处理索引' ,在the_data [index]中指定
作为wicket:
print'wicket equals',wicket
#OK - 我可以看到Innings。现在,我如何获得
#并获得小门?


解决方案

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

 输入数据:
输入['innings']:
打印['wickets']

打印:

 >>>数据输入:
...输入['innings']:
...打印['wickets']
...
10
9
0
0

每个级别也是:

 >>>用于输入数据:
...打印输入['description']
...对于我,在枚举(输入['innings']):
...打印' Innings {}:{} wickets'.format(i + 1,inning ['wickets'])
...
斯里兰卡其他地区v斯里兰卡A 2013年5月14日Pallekele
局1:10门票
局2:9门票
第63场比赛:皇家挑战者班加罗尔v国王XI旁遮普邦在班加罗尔,2013年5月14日
局1:0门票
第2局比赛:0比赛门票
第64场比赛:Chennai超级国王v德里Daredevils,2013年5月14日,晨奈


I'm new to Python (last week), and have reached my limit. Spent three days on this, most of my time in stackoverflow, but I cannot work out how to go any further!

The Json has multiple nested arrays. It could contain three (as the example below (json.txt) does), or 30. I need to loop through each, then drill down to 'innings' and finally get the value of 'wickets'. It's this last step that I'm confused by. Can anyone advise?

Yours in total desperation

Will

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

解决方案

First of all, don't use an index but loop directly over the lists; that way you can give them meaningful names. The top-level is a list of entries, each entry is a dictionary with a 'innings' key, and each innings is a list of dictionaries, with, among others, a wickets key:

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

This prints:

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

This makes it easier to add information at each level too:

>>> 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

这篇关于用多个'for'循环解码嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆