Merhaba, json dosya oluştururken belirli bir kural var mı?
Farklı dosya isimleriyle oluşturmaya çalışıyorum. Hata veriyor… (Hata mesajını en sona koydum çok karışıklık olmasın diye)
Mesela az önce “users.json” oluşturabiliyordum ama şuan users.json oluştururken hata veriyor Evet atladığım bir şey var sanrıım da ne?
Bunu neden soruyorum ;
Örnek 1 ve hata ;
if os.path.exists('db.json'):
with open('db.json', 'r', encoding='utf-8') as file:
users = json.load(file)
for user in users:
user = json.loads(user)
newUser = User(username = user['username'], password = user['password'], email = user['email'])
self.users.append(newUser)
print(self.users)
else:
print("Dosya yok.")
Ornek 2 ; herhangi bir hata yok…
def loadUsers(self):
# Dosya kontrol
if os.path.exists('ornek.json'):
with open('ornek.json', 'r', encoding='utf-8') as file:
users = json.load(file)
for user in users:
user = json.loads(user)
newUser = User(username = user['username'], password = user['password'], email = user['email'])
self.users.append(newUser)
print(self.users)
else:
print("Dosya yok.")
Ornek 3 ve yine hata
def loadUsers(self):
# Dosya kontrol
if os.path.exists('GeneralInformation.json'):
with open('GeneralInformation.json', 'r', encoding='utf-8') as file:
users = json.load(file)
for user in users:
user = json.loads(user)
newUser = User(username = user['username'], password = user['password'], email = user['email'])
self.users.append(newUser)
print(self.users)
else:
print("Dosya yok.")
Hata mesajı ;
Traceback (most recent call last):
File "f:\Education\ZyroServer\AccountKayitTest.py", line 50, in <module>
repository = UserRepository()
File "f:\Education\ZyroServer\AccountKayitTest.py", line 15, in __init__
self.loadUsers()
File "f:\Education\ZyroServer\AccountKayitTest.py", line 22, in loadUsers
users = json.load(file)
File "C:\Users\bilgi\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\bilgi\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\bilgi\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\bilgi\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)