如何从JSON输出中嵌套元组中的键/值来获取对象?

发布于 2025-01-31 09:15:00 字数 1531 浏览 3 评论 0原文

如何从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2025-02-07 09:15:00

根据先前的答案,这几乎是正确的。必须是

output = json.loads(输出[0])

According previous answer, it's almost correct. Must be

output = json.loads(output[0])

灵芸 2025-02-07 09:15:00

如错误所说,变量输出必须是类型bytes字符串,或bytearray

如果我们更深入地看,则元组实际上只包含一个String元素。因此,我相信我们应该得到字符串元素!

我们可以使用此操作:

output = json.loads(output)[0] # Tuples are just like arrays

因此,现在变量输出是类型String

如果这不起作用,也许尝试重命名output变量:

variable = json.loads(output)[0]

对不起,如果这不正确!

As the error says, the variable output must be of type bytes, string, or bytearray.

If we take a deeper look, the tuple actually only contains one string element. So, I believe we're supposed to get that string element!

We can do so using this:

output = json.loads(output)[0] # Tuples are just like arrays

So, now the variable output is of type string!

If this doesn't work, perhaps try renaming the output variable as such:

variable = json.loads(output)[0]

Sorry if this is incorrect!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文