gpt4 book ai didi

python - 无法修复 JSONDecodeError : Invalid control character

转载 作者:行者123 更新时间:2023-12-01 07:58:44 28 4
gpt4 key购买 nike

我有一个 json 文件,其中包含以下内容:

#create test.json and add content
dbutils.fs.put("test.json",'{"type": "abc","project_id": "abc","private_key_id": "123","private_key": "-----BEGIN PRIVATE KEY----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCJ05O/Ke3tGA96\n-----END PRIVATE KEY-----\n","client_email": "abc@gmail.com","client_id": "345","auth_uri": "https://accounts.abc.com/o/oauth2/auth","token_uri": "https://abc","auth_provider_x509_cert_url": "https://abc","client_x509_cert_url": "https://abcrobot/v1/metadata/x509"}'

#Read test.json
json.loads(test.json)

#Error
JSONDecodeError: Invalid control character

我做错了什么?

最佳答案

不要手动生成 JSON;让 json.dumps 为您做这件事。这将确保私钥中出现的换行符被正确编码。

config = {
"type": "abc",
"id": "t1-2-3",
"private_key_id": "123",
"private_key": "-----BEGIN PRIVATE KEY----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwkO/V+WyyrmTFVFoDN9sN1+AL+KX/IB3y\nZqLJVPGCdQ1l+TlZXbFKFYMwo/Ca4N8g4sy7ZH/9UcqKTxawyqrh7dPcRWWI\nyvAWaEJVCtZhw+Hp4fkmWdoi\n-----END PRIVATE"
}

dbutils.fs.put("test.json", json.dumps(config))

with open("test.json") as f:
config2 = json.load(f)

关于python - 无法修复 JSONDecodeError : Invalid control character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55818946/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com