[英]I keep getting the following error trying to parse json in python “string indices must be integers”
[英]Getting an error in JSON Import with python “string indices must be integers”
import json
json_dump = json.dumps('1.json')
json_object = json.loads(json_dump)
print(json_object["client_id"])
我正在 python 中編寫代碼,該代碼從 json 文件中提取 email id。 但我收到一個錯誤:-
" 字符串索引必須是整數 "
這是我正在導入的 JSON 文件:-
{
"type": "Some Data",
"project_id": "Some Data",
"private_key_id": "Some Data",
"private_key": "Some Data",
"client_email": "Some Data",
"client_id": "Some Data",
"auth_uri": "Some Data",
"token_uri": "Some Data",
"auth_provider_x509_cert_url": "Some Data",
"client_x509_cert_url": "Some Data"
}
您應該首先打開文件,並將打開的文件傳遞給json.load(file)
import json
# opening the json file
json_file = open('1.json','r+')
json_object = json.load(json_file)
print(json_object["client_id"])
Output:
Some Data
用於循環命名為數字的 json 文件
import json
# opening the json file
# files named as 1.json to 3.json will do this
for i in range(1,4):
json_file = open(str(i)+'.json','r+')
json_object = json.load(json_file)
print(json_object["client_id"])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.