英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论