将RESTful API响应从列表转换为字典

2024-06-26 01:36:06 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我有一个函数(如下所示),它从我自己创建的API发出GET请求

def get_vehicles(self):
    result = "http://127.0.0.1:8000/vehicles" 
    response = requests.get(result)
    data = response.content
    data_dict = json.loads(data)
    return data_dict

我得到的数据是这种格式的。哪一个是字典的列表

data_dict = [{'colour': 'Black', 'cost': 10, 'latitude': -37.806152, 'longitude': 144.95787, 'rentalStatus': 'True', 'seats': 4, 'user': None, 'vehicleBrand': 'Toyota', 'vehicleID': 1, 'vehicleModel': 'Altis'}, {'colour': 'White', 'cost': 15, 'latitude': -37.803913, 'longitude': 144.964859, 'rentalStatus': 'False', 'seats': 4, 'user': {'firstname': 'Test', 'imageName': None, 'password': 'password', 'surname': 'Ing', 'userID': 15, 'username': 'Testing'}, 'vehicleBrand': 'Honda', 'vehicleID': 3, 'vehicleModel': 'Civic'}]

有可能把它转换成一本字典吗?例如:

data_dict = {'colour': 'Black', 'cost': 10, 'latitude': -37.806152, 'longitude': 144.95787, 'rentalStatus': 'True', 'seats': 4, 'user': None, 'vehicleBrand': 'Toyota', 'vehicleID': 1, 'vehicleModel': 'Altis'}, {'colour': 'White', 'cost': 15, 'latitude': -37.803913, 'longitude': 144.964859, 'rentalStatus': 'False', 'seats': 4, 'user': {'firstname': 'Test', 'imageName': None, 'password': 'password', 'surname': 'Ing', 'userID': 15, 'username': 'Testing'}, 'vehicleBrand': 'Honda', 'vehicleID': 3, 'vehicleModel': 'Civic'}

Tags: nonedatagetpassworddictcostcolourlatitude
2条回答

不,第二个结果是元组,而不是dict

data_dict = {'colour': 'Black', 'cost': 10, 'latitude': -37.806152, 'longitude': 144.95787, 'rentalStatus': 'True', 'seats': 4, 'user': None, 'vehicleBrand': 'Toyota', 'vehicleID': 1, 'vehicleModel': 'Altis'}, {'colour': 'White', 'cost': 15, 'latitude': -37.803913, 'longitude': 144.964859, 'rentalStatus': 'False', 'seats': 4, 'user': {'firstname': 'Test', 'imageName': None, 'password': 'password', 'surname': 'Ing', 'userID': 15, 'username': 'Testing'}, 'vehicleBrand': 'Honda', 'vehicleID': 3, 'vehicleModel': 'Civic'}
print(type(data_dict))
# <class 'tuple'>

这与:

data_dict = ({'colour': 'Black', 'cost': 10, 'latitude': -37.806152, 'longitude': 144.95787, 'rentalStatus': 'True', 'seats': 4, 'user': None, 'vehicleBrand': 'Toyota', 'vehicleID': 1, 'vehicleModel': 'Altis'}, {'colour': 'White', 'cost': 15, 'latitude': -37.803913, 'longitude': 144.964859, 'rentalStatus': 'False', 'seats': 4, 'user': {'firstname': 'Test', 'imageName': None, 'password': 'password', 'surname': 'Ing', 'userID': 15, 'username': 'Testing'}, 'vehicleBrand': 'Honda', 'vehicleID': 3, 'vehicleModel': 'Civic'})

这就是为什么它是一个元组

如果您只想在dict中合并它们,这似乎是不可能的,因为dict不能有相同的键。但是您可以将值合并为一个列表,如:

d = {key: list(value) for key, value in zip(data_dict[0].keys(), zip(data_dict[0].values(), data_dict[1].values()))}
print(d)

结果(确保它们具有相同的长度):

{
    'colour': ['Black', 'White'],
    'cost': [10, 15],
    'latitude': [-37.806152, -37.803913],
    'longitude': [144.95787, 144.964859],
    'rentalStatus': ['True', 'False'],
    'seats': [4, 4],
    'user': [None, {
        'firstname': 'Test',
        'imageName': None,
        'password': 'password',
        'surname': 'Ing',
        'userID': 15,
        'username': 'Testing'
    }],
    'vehicleBrand': ['Toyota', 'Honda'],
    'vehicleID': [1, 3],
    'vehicleModel': ['Altis', 'Civic']
}

这是一份字典清单

因此,您可以使用数组语法来访问它们:data_dict[0]例如,对于第一个元素

相关问题 更多 >