你可以如何在Python中更改JSON格式。

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

How can i change the JSON format in Python

问题

我的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......
                }
            ]
        }
    }
}

所以简而言之,我想要去掉开头和结尾的"以及\符号。

英文:

My JSON files looks like this

"{\"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.

答案1

得分: 2

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

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)

答案2

得分: 0

import json

myUnfomattedJSON = "..."
jsonObj = json.loads(myUnformattedJSON)

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

print(formattedJSON)

英文:
import json

myUnfomattedJSON = "..."

jsonObj = json.loads(myUnformattedJSON)

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

print(formattedJSON)

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

发表评论

匿名网友

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

确定