2017-10-08 84 views
0

我試圖訪問嵌套的JSON文件,並通過將它們導入python字典來打印它的數據。當我將它們打印出來時,最後一個值是來自字典的int值。從Python嵌套字典中打印int值,'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簽到[ 「時間」] [天] [時間]:TypeError:'int'對象不可迭代

回答

1

最後一個循環沒有意義......沒有任何東西在那裏循環。 (值爲,如錯誤所示,int。)我想你可能想要這樣的東西:

for day in d["time"]: 
    for time in d["time"][day]: 
     checkin = d["time"][day][time] 
     print(day, time, checkin)