我想从联机https://api.myjson.com/bins/y2k4y读取json文件,并基于参数“ipv4”、“ipv6”进行打印。我已经用python编写了代码。它向我展示了这个错误。请建议我解决这个问题
Python代码
import requests
import json
# Method To Get REST Data In JSON Format
def getResponse(url,choice):
response = requests.get(url)
if(response.ok):
jData = json.loads(response.content)
if(choice=="deviceInfo"):
print("working")
deviceInformation(jData)
else:
print("NOT working")
response.raise_for_status()
# Parses JSON Data To Find Switch Connected To H4
def deviceInformation(data):
global switch
global deviceMAC
global hostPorts
switchDPID = ""
print ( data)
for i in data:
print ("i: ", i['ipv4'])
deviceInfo = "https://api.myjson.com/bins/y2k4y"
getResponse(deviceInfo,"deviceInfo")
错误
File "E:/aaa/e.py", line 27, in deviceInformation
print ("i: ", i['ipv4'])
TypeError: string indices must be integers
尝试使用
for i in data['devices']:
而不是for i in data:
jData
是一个dict
只有一个键devices
devices
包含您需要的所有其他信息将
for i in data
更改为for i in data['devices']
:相关问题 更多 >
编程相关推荐