Python3处理嵌套JSON响应的方法是什么?

huangapple go评论89阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月6日 22:37:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定