我试图打印出JSON数据的所有cpe_mac
字段。在Python中访问JSON密钥
我
# Last updated : BH | 8/31/2016
import requests
import json
ssc_ip = raw_input("What is your SSC Host (Ex. http://172.19.242.32:1234/) ? : ")
if not ssc_ip:
ssc_ip = 'http://172.19.242.32:1234/'
cpe_num = raw_input("How many CPE(s) you want to delete ? : ")
print '\n'
url = ssc_ip+'vse/vcpes'
json_data = requests.get(url).json()
# print json_data
for x in json_data:
print json_data.cpe_mac
我一直得到
AttributeError: 'dict' object has no attribute 'cpe_mac'
试图
print json_data['cpe_mac']
我
KeyError: 'cpe_mac'
。此外,您正在遍历'json_data',但不使用迭代对象。 –
你需要使用字典符号:'json_data ['cpe_mac']'但是你期待'x'做什么? –
@ TadhgMcDonald-Jensen:是的,我得到了'KeyError:'cpe_mac'' – ihue