简体   繁体   English

如何在 Python 中更改 JSON 格式

[英]How can i change the JSON format in Python

My JSON files looks like this我的 JSON 文件看起来像这样

"{\"GetEventHeadlines_Response_1\":{\"EventHeadlines\": 
{\"Headline\":[{\"CountryCode\":\"US\",\"EventType\":\"EarningsCallsAndPresentations\",
\"Duration\":
{\"EndDateTime\":\"2019-12-30T12:00:00\",\"EndQualifier\":\"None\", \"IsEstimate\":false,\"StartDateTime\":\
"2019-12-30T12:00:00\",\"StartQualifier\":
\"DateTime\"},\"EventId\":12969284......

I want to change this to我想把它改成

{
"GetEventHeadlines_Response_1":{
"EventHeadlines":{
  "Headline":[
    {
      "CountryCode":"US",
      "Duration":{
        "EndDateTime":"2019-12-30T12:00:00",
        "EndQualifier":"None",
        "IsEstimate":false,
        "StartDateTime":"2019-12-30T12:00:00",
        "StartQualifier":"DateTime"
      },
      "EventId":12969284,.....

So in short i want to get rid of the ""(only at the beginning and end ) and the \\ sign.所以简而言之,我想摆脱“”(仅在开头和结尾)和 \\ 符号。

import json

data = '''
        {\"GetEventHeadlines_Response_1\":{\"EventHeadlines\": 
{\"Headline\":[{\"CountryCode\":\"US\",\"EventType\":\"EarningsCallsAndPresentations\",
\"Duration\":
{\"EndDateTime\":\"2019-12-30T12:00:00\",\"EndQualifier\":\"None\", \"IsEstimate\":false,\"StartDateTime\":\
"2019-12-30T12:00:00\",\"StartQualifier\":
\"DateTime\"}
       '''


data = json.loads(json.dumps(data))
print(data)
import json

myUnfomattedJSON = "..."

jsonObj = json.loads(myUnformattedJSON)

formattedJSON = json.dumps(jsonObj, indent=2)

print(formattedJSON)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM