在把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数据是不允许空格的,不能是单引号,必须是双引号。