【发布时间】:2020-11-08 16:41:38
【问题描述】:
我有这段代码,但我的 JSON 文件中有一些带有空行的行。我得到这个错误。 这是一个自定义命令,我收到此错误。我想在我的 Django 应用程序的数据库中创建一个作业列表,我正在使用 For 循环。非常感谢您的帮助
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
from django.core.management.base import BaseCommand
from jobs.models import Job
import json
class Command(BaseCommand):
help = 'Set up the database'
def handle(self, *args: str, **options: str):
with open('static/joblist03112020.json', 'r') as handle:
for line in handle.readlines():
print(line)
line = json.loads(line)
existing_job = Job.objects.filter(
job_title = line['job_title'],
company = line['company'],
company_url = line['company_url'],
description = line['description'],
salary = line['salary'],
city = line['city'],
district = line['district'],
url = line['url'],
job_type = line['job_type'],
)
if existing_job.exists() is False:
Job.objects.create(
job_title = line['job_title'],
company = line['company'],
company_url = line['company_url'],
description = line['description'],
salary = line['salary'],
city = line['city'],
district = line['district'],
url = line['url'],
job_type = line['job_type'],
)
Job.save()
self.stdout.write(self.style.SUCCES('added jobs!add'))
【问题讨论】: