从 json 输出中删除一个字符
Removing a character from json output
这是我将收到的 JSON 输出:
{
"status": "success",
"data": {
"@Response": "1",
"@ErrorNumber": "0",
"@ErrorDescription": "",
"@ErrorDisplay": "1",
"Transactions": [
{
"@Account": "xxxxxxxxxx055819",
"@TranDate": "04/09/2020",
"@PostDate": "04/09/2020",
"@Description": "ValueLoad",
"@Reference": "",
"@Amt": "50"
},
{
"@Account": "xxxxxxxxxx055819",
"@TranDate": "04/22/2020",
"@PostDate": "04/22/2020",
"@Description": "Balance Adjustment Debit Cardholder",
"@Reference": "",
"@Amt": "-10"
}
]
},
"meta": {}
}
但是,我们需要将这段代码传递给我们的前端,@符号会导致变量转换错误。
如何去掉下面变量中的@符号?
谢谢!
您可以尝试使用正则表达式只获取您需要的内容并删除其余内容。
你可以这样做。如果您的 json 存储在 initial_json 变量中,这将删除所有 @ 符号,
import re
import json
final_json = json.loads(re.sub('@', '', json.dumps(initial_json)))
这是我将收到的 JSON 输出:
{
"status": "success",
"data": {
"@Response": "1",
"@ErrorNumber": "0",
"@ErrorDescription": "",
"@ErrorDisplay": "1",
"Transactions": [
{
"@Account": "xxxxxxxxxx055819",
"@TranDate": "04/09/2020",
"@PostDate": "04/09/2020",
"@Description": "ValueLoad",
"@Reference": "",
"@Amt": "50"
},
{
"@Account": "xxxxxxxxxx055819",
"@TranDate": "04/22/2020",
"@PostDate": "04/22/2020",
"@Description": "Balance Adjustment Debit Cardholder",
"@Reference": "",
"@Amt": "-10"
}
]
},
"meta": {}
}
但是,我们需要将这段代码传递给我们的前端,@符号会导致变量转换错误。
如何去掉下面变量中的@符号?
谢谢!
您可以尝试使用正则表达式只获取您需要的内容并删除其余内容。
你可以这样做。如果您的 json 存储在 initial_json 变量中,这将删除所有 @ 符号,
import re
import json
final_json = json.loads(re.sub('@', '', json.dumps(initial_json)))