gpt4 book ai didi

django - 在 Django 单元测试中访问 JsonResponse 中的键值对

转载 作者:行者123 更新时间:2023-11-30 23:49:02 24 4
gpt4 key购买 nike

我想访问以下 jsonResponse 对象中返回的数据:

{"results": [[1, "Probability and Stochastic Processes", 9781118324561, "Roy D. Yates", "2014-01-01", "Wiley"], [2, "Interaction Design", 9781119020752, "Rogers Price", "2015-01-01", "John Wiley & Sons"], [3, "Microeconomics", 9780077501808, "Colander", "2013-01-01", "McGraw Hill"], [4, "jfalksdjf", 123123, "test", "1990-01-01", "Penguin"]]}

然而,我遇到了麻烦,我尝试了很多事情
def test_noIDGiven(self):
response = self.client.get(reverse('allTextbooks')) #returns the json array above
#check that there are three textbooks in the response
#print(response.content['results'][0][0]) - this didnt work
self.assertEquals(response.content[0][0], 1) #basically want to access the id of the first object and make sure it is 1

关于如何以最佳方式访问此对象的键值对的任何帮助都会很好。提前致谢

更多信息:
- 当我反转 'allTextbooks' 时,api 调用返回:
results = list(Textbook.objects.values_list())
return JsonResponse({'results': results})

最佳答案

从 Django 2.0 开始,您可以直接访问 JSON 响应。

 self.assertEqual(response.json()['results'][0][0], 1)

关于django - 在 Django 单元测试中访问 JsonResponse 中的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42472353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com