更新:问题已解决。我需要指定超级文件夹,因此我将'data.json'
更改为读取'folderName/data.json'
Django 2.1.2 Python 3.6
尝试在views.py中加载Json文件时,我不断收到JSONDecodeError。
views.py
from django.shortcuts import render, HttpResponse
import json
def index(request):
with open('data.json') as data:
args = json.loads(data.read())
return render(request, 'folderName/index.html', args)
data.json
{"number2":{
"type":"Holiday",
"content": [
{"name":"New Year's Day", "month":1, "day":1},
{"name":"Martin Luther King Jr. Day", "month":1, "day":21},
{"name":"National Wear Red Day", "month":2, "day":1},
{"name":"Groundhog Day", "month":2, "day":2},
{"name":"Valentine's Day", "month":2, "day":14},
{"name":"Presient's Day", "month":2, "day":18},
{"name":"Employee Appreciation Day", "month":3, "day":1},
{"name":"Daylight Savings Time", "month":3, "day":10},
{"name":"St. Patrick's Day", "month":3, "day":17},
{"name":"Good Friday", "month":4, "day":19},
{"name":"Easter Sunday", "month":4, "day":21},
{"name":"Cinco De Mayo", "month":5, "day":5},
{"name":"Mother's Day", "month":5, "day":12},
{"name":"Memorial Day", "month":5, "day":27},
{"name":"Flag Day", "month":6, "day":14},
{"name":"Father's Day", "month":6, "day":16},
{"name":"Independence Day", "month":7, "day":4},
{"name":"Labor Day", "month":9, "day":2},
{"name":"Columbus Day", "month":10, "day":14},
{"name":"Halloween", "month":10, "day":31},
{"name":"Daylight Savings Time", "month":11, "day":3},
{"name":"Vetern's Day", "month":11, "day":11},
{"name":"Thanksgiving Day", "month":11, "day":28},
{"name":"Black Friday", "month":11, "day":29},
{"name":"Cyber Monday", "month":12, "day":2},
{"name":"Pearl Harbor Rembrance Day", "month":12, "day":7},
{"name":"Christmas Eve", "month":12, "day":24},
{"name":"Christmas Day", "month":12, "day":25},
{"name":"New Year's Eve", "month":12, "day":31}
]
}}
当我在空闲状态运行相同的代码时,它将在单独的py文件中解码。
import json
with open('data.json') as data:
args = json.loads(data.read())
edit 1:根据要求,我添加了完整的追溯,希望对您有所帮助。我总是可以使用sql数据库,但是我认为,如果我也需要附加更多数据,则编辑json文件会更容易。
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/AnkenyIA/
Django Version: 2.1.2
Python Version: 3.6.0
Installed Applications:
['folderName',
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File
"C:\Users\$user\AppData\Local\Programs\Python\Python36\lib\json\decoder.py"
in raw_decode
355. obj, end = self.scan_once(s, idx)
During handling of the above exception (0), another exception occurred:
File "C:\Users\$user\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\$user\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e,
request)
File "C:\Users\$user\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "C:\Users\$user\Desktop\mysite\mysite\folderName\views.py" in index
7. data = json.load(file)
File
"C:\Users\$user\AppData\Local\Programs\Python\Python36\lib\json\__init__.py"
in load
299. parse_constant=parse_constant,
object_pairs_hook=object_pairs_hook, **kw)
File
"C:\Users\$user\AppData\Local\Programs\Python\Python36\lib\json\__init__.py"
in loads
354. return _default_decoder.decode(s)
File
"C:\Users\$user\AppData\Local\Programs\Python\Python36\lib\json\decoder.py"
in decode
339. obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File
"C:\Users\$user\AppData\Local\Programs\Python\Python36\lib\json\decoder.py"
in raw_decode
357. raise JSONDecodeError("Expecting value", s, err.value)
from None
Exception Type: JSONDecodeError at /folderName/
Exception Value: Expecting value: line 1 column 1 (char 0)