python-3.x Structure表中的JSON数据

vcirk6k6  于 2023-04-08  发布在  Python
关注(0)|答案(1)|浏览(97)

我从JSON中提取数据,现在,但我需要在一个表格格式的结果。我错了,请帮助我

import requests
import json

 url = "https://pmod.ccdevprojects.com/api/v1/budgets"
 response = requests.get(url)

 if response.status_code == 200:
if response.headers['content-type'] == 'application/json':
    data = json.loads(response.text)
    formatted_data = json.dumps(data, indent=4)
    print(formatted_data)
else:
    raise Exception("Response content type is not JSON")
else:
print("Error: Unable to retrieve data from API.")

我需要这样的输出

ID    Category          _Value   Count
135   Remaining Budget  Null       3
136   Remaining Budget  9216.0     3
qacovj5a

qacovj5a1#

在python中,精确的缩进是至关重要的

如果像这样布置就能用

import requests
import json

url = "https://pmod.ccdevprojects.com/api/v1/budgets"
response = requests.get(url)

if response.status_code == 200:
  if response.headers['content-type'] == 'application/json':
      data = json.loads(response.text)
      formatted_data = json.dumps(data, indent=4)
      print(formatted_data)
  else:
      raise Exception("Response content type is not JSON")
else:
  print("Error: Unable to retrieve data from API.")

工作输出的证据:

相关问题