英文:
What is the Python3 approach to process a nested JSON response?
问题
I'm sorry, but it seems that the text you provided is already in English, and there's no need for translation. If you have any questions or need assistance with this code, please feel free to ask.
英文:
In running the following code, I am interested in the "Report_ForecastIncomeSummary" dictionary. However, I have not yet been able to figure out how to use that dictionary. I apologize for my ignorance and perhaps I am approaching this incorrectly.
My Code:
import requests
import json
# Note, the http://x.x.x.x:4500/api/ezapp/getdata bit in my code
# does contain the correct IP and port number. It has been masked here.
ezAPI = 'http://x.x.x.x:4500/api/ezapp/getdata'
myData = {'Request': "{'Request':'Report_ForecastIncomeSummary','DateFrom':'1/3/2020','DateThru':'1/3/2020'}"}
response = requests.post(ezAPI, json=myData)
data = json.loads(response)
Response:
{"Success":true,"FailureInformation":"","Result":"{\"Request\": \"Report_ForecastIncomeSummary\", \r\n\"status\": \"OK\",\r\n\"Report_ForecastIncomeSummary\": [\r\n{\"TicketState\":\"future\",\"AmountScheduled\":\"144.45\",\"AmountUnscheduled\":\"0\",\"amount\":\"144.45\"},\r\n{\"TicketState\":\"invoiced\",\"AmountScheduled\":\"0\",\"AmountUnscheduled\":\"380\",\"amount\":\"380\"},\r\n{\"TicketState\":\"ticket\",\"AmountScheduled\":\"3846.73\",\"AmountUnscheduled\":\"401\",\"amount\":\"4247.73\"}\r\n] }"}
Also, I can't figure out why the escape characters are returned ... not sure if that's part of my problem.
Again, the part of this response I want to store as a dictionary is:
"Report_ForecastIncomeSummary\": [\r\n{\"TicketState\":\"future\",\"AmountScheduled\":\"144.45\",\"AmountUnscheduled\":\"0\",\"amount\":\"144.45\"},\r\n{\"TicketState\":\"invoiced\",\"AmountScheduled\":\"0\",\"AmountUnscheduled\":\"380\",\"amount\":\"380\"},\r\n{\"TicketState\":\"ticket\",\"AmountScheduled\":\"3846.73\",\"AmountUnscheduled\":\"401\",\"amount\":\"4247.73\"}\r\n]
Appreciate any assistance or guidance. I am not using flask here just python locally, aside from the API call. Also, in using Stackoverflow as a resource over the years I know there is a lot of focus on researching it first and articulating the issue correctly. I apologize in advance if I broke any cardinal rules. Thank you.
答案1
得分: 0
requests
带有一个令人惊奇的功能.json()
:
您只需编写:
response = requests.post(ezAPI, json=myData).json()
然后您将获得一个Python字典。
英文:
requests comes with an amazing feature .json()
:
You can just write :
response = requests.post(ezAPI, json=myData).json()
And you'll get a python dic
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论