在把json數據格式轉化爲字典的時候,報錯:
代碼部分是這樣的:
from flask import Flask,render_template
import os,json
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELODE']=True
@app.route('/')
def index():
file_json = {}
strpath = "/home/shiyanlou/files"
for path,d,filelist in os.walk(strpath):
for filename in filelist:
file_suffix = filename.split('.')
if(file_suffix[1] == 'json'):
absfilename = path + '/' + filename
with open(absfilename,'r') as fopen:
filejson[filename] = json.loads(fopen.read())
return render_template('file.html',file_json=file_json)
解析是沒錯的,但結果報錯了,後來找了很久的原因,才發現自己的json數據裏有空格。嚴格來說json數據是不允許空格的,不能是單引號,必須是雙引號。