Python 如何将JSON API数据绑定到一个字典中?
我试图使用标准python库将API数据打包成一个GET请求Python 如何将JSON API数据绑定到一个字典中?,python,json,django,dictionary,django-rest-framework,Python,Json,Django,Dictionary,Django Rest Framework,我试图使用标准python库将API数据打包成一个GET请求 class GetData(APIView): def get(self, request, *args, **kwargs): urls = [url_1, url_2, url_3, url_4 ] data_bundle = [] f
class GetData(APIView):
def get(self, request, *args, **kwargs):
urls = [url_1,
url_2,
url_3,
url_4
]
data_bundle = []
for x in urls:
response = requests.get(x, headers={'Content-Type': 'application/json'}).json()
data_bundle.append(response)
return Response(data_bundle, status=status.HTTP_200_OK)
返回的响应必须是JSON数据,我正在尝试让它工作,但是响应数据似乎相互过度隐藏了?如何正确创建JSON字典
我尝试将数据包
切换到空字典而不是列表。但是,这只是导致了一个错误,即:
ValueError:字典更新序列元素#0的长度为8;2是必需的
有没有一个简单的方法来完成这一点,我错过了?
谢谢你的帮助
class GetData(APIView):
def get(self, request, *args, **kwargs):
urls = [url_1,
url_2,
url_3,
url_4
]
data_bundle = []
for x in urls:
response = requests.get(x, headers={'Content-Type': 'application/json'}).json()
data_bundle.append(response)
return Response(data_bundle, status=status.HTTP_200_OK)
也许这将有助于避免在循环体中使用返回。数据不会相互覆盖
也许这将有助于避免在循环体中使用返回。数据不会相互覆盖你能举个例子说明你得到的数据是什么样的,你希望它是什么样的吗?你能举个例子说明你得到的数据是什么样的,你希望它是什么样的吗?啊,是的,这样一个愚蠢的错误,谢谢你帮我找出哪里出了问题。啊,是的,真是个愚蠢的错误,谢谢你帮我找出问题所在。