我试图访问嵌套的JSON文件并通过将它们导入python字典来打印它的数据。当我打印出来时,最后一个值的错误是字典中的int值。
这是我的JSON数据
{
"time": {
"Thursday": {
"21:00": 4,
"1:00": 1,
"4:00": 1,
"2:00": 1,
"20:00": 2,
"22:00": 1,
"19:00": 1,
"15:00": 2,
"13:00": 1,
"23:00": 2
},
"Wednesday": {
"11:00": 2,
"13:00": 2,
"14:00": 1,
"17:00": 1,
"6:00": 1,
"2:00": 1,
"0:00": 2,
"1:00": 1,
"21:00": 1,
"18:00": 1,
"19:00": 1,
"20:00": 2
},
"Sunday": {
"18:00": 1,
"16:00": 1,
"14:00": 1,
"19:00": 2,
"17:00": 1,
"23:00": 1,
"21:00": 1,
"20:00": 5,
"6:00": 1,
"0:00": 1,
"2:00": 2,
"3:00": 3
},
"Friday": {
"16:00": 1,
"14:00": 2,
"10:00": 2,
"23:00": 1,
"19:00": 2,
"18:00": 1,
"15:00": 1,
"21:00": 2,
"22:00": 2,
"3:00": 1,
"0:00": 2
},
"Saturday": {
"21:00": 1,
"23:00": 3,
"18:00": 4,
"10:00": 1,
"12:00": 1,
"13:00": 3,
"14:00": 1,
"15:00": 1,
"16:00": 2,
"17:00": 3,
"2:00": 1,
"0:00": 1,
"1:00": 2
},
"Monday": {
"12:00": 1,
"11:00": 1,
"14:00": 1,
"18:00": 1,
"19:00": 1,
"23:00": 1,
"20:00": 1
},
"Tuesday": {
"18:00": 2,
"12:00": 1,
"13:00": 2,
"16:00": 1,
"15:00": 1,
"4:00": 1,
"21:00": 1,
"20:00": 2,
"23:00": 2
}
},
"business_id": "7KPBkxAOEtb3QeIL9PEErg"
}
这是我的python代码:
import json
with open('dataset/sample-checkin.json') as json_data:
d = json.load(json_data)
for day in d["time"]:
for time in d["time"][day]:
for checkin in d["time"][day][time]:
print(day, time, checkin)
错误:在d [" time"] [day] [time]中签入:TypeError:' int'对象不可迭代
答案 0 :(得分:1)
最后一个循环没有意义......那里没有什么可循环的。 (正如错误所说,值是int
。)我想你可能想要这样的东西:
for day in d["time"]:
for time in d["time"][day]:
checkin = d["time"][day][time]
print(day, time, checkin)