如何从JSON输出中嵌套元组中的键/值来获取对象?
如何从JSON数据中嵌套在元组中的键/值来获取对象?我一直在尝试获取以下API请求的JSON输出并加载了JSON输出 - 但是我最终遇到了此错误,
the JSON object must be str, bytes or bytearray, not tuple
我写了这样的代码:
output = json.loads(output)
print(output)
我得到的输出
('{\n "architecture" : "x86_64",\n "billingProducts" : null,\n "devpayProductCodes" : null,\n "marketplaceProductCodes" : null,\n "imageId" : "****",\n "instanceId" : "***",\n "instanceType" : "t3.2xlarge",\n "kernelId" : null,\n "pendingTime" : "2022-05-19T17:32:35Z",\n "privateIp" : "***",\n "ramdiskId" : null,\n "version" : "2017-09-30"\n}', ' % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\n 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n100 56 100 56 0 0 56000 0 --:--:-- --:--:-- --:--:-- 56000\n % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\n 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n100 479 100 479 0 0 467k 0 --:--:-- --:--:-- --:--:-- 467k\n')
output = json.loads(output)
File "/usr/lib/python3.8/json/__init__.py", line 341, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not tuple
how to get the object with keys/value nested inside tuple from a Json data ? I have been trying to get the below Json output for an api request and loaded the Json output - but i end up with this error
the JSON object must be str, bytes or bytearray, not tuple
i wrote a code like this:
output = json.loads(output)
print(output)
The output i get
('{\n "architecture" : "x86_64",\n "billingProducts" : null,\n "devpayProductCodes" : null,\n "marketplaceProductCodes" : null,\n "imageId" : "****",\n "instanceId" : "***",\n "instanceType" : "t3.2xlarge",\n "kernelId" : null,\n "pendingTime" : "2022-05-19T17:32:35Z",\n "privateIp" : "***",\n "ramdiskId" : null,\n "version" : "2017-09-30"\n}', ' % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\n 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n100 56 100 56 0 0 56000 0 --:--:-- --:--:-- --:--:-- 56000\n % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\n 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n100 479 100 479 0 0 467k 0 --:--:-- --:--:-- --:--:-- 467k\n')
output = json.loads(output)
File "/usr/lib/python3.8/json/__init__.py", line 341, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not tuple
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据先前的答案,这几乎是正确的。必须是
According previous answer, it's almost correct. Must be
如错误所说,变量
输出
必须是类型bytes
,字符串
,或bytearray
。如果我们更深入地看,则元组实际上只包含一个
String
元素。因此,我相信我们应该得到字符串
元素!我们可以使用此操作:
因此,现在变量
输出
是类型String
!如果这不起作用,也许尝试重命名
output
变量:对不起,如果这不正确!
As the error says, the variable
output
must be of typebytes
,string
, orbytearray
.If we take a deeper look, the tuple actually only contains one
string
element. So, I believe we're supposed to get thatstring
element!We can do so using this:
So, now the variable
output
is of typestring
!If this doesn't work, perhaps try renaming the
output
variable as such:Sorry if this is incorrect!