0

在这个文件中,我创建了一个 JSON 文件。

This is my JSON.config { {
  "params":
  {
    "upload_location":"E:\\Ineuron Internship\\Automated ML\\AutoML\\uploads"


  },
}

在这个文件中,我编写了用于加载 JSON 文件的代码。

import json
from flask import request, Flask, render_template
import os
from werkzeug.utils import secure_filename
os.putenv('LANG', 'en_US.UTF-8')




os.putenv('LC_ALL', 'en_US.UTF-8')

with open('config.json','r')as E:
     params = json.load(E)



app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = params['upload_location']



@app.route("/", methods=['GET', 'POST'])
def home():
    return render_template('index.html')

@app.route("/uploader",methods = ['GET','POST'])
def uploader():
    if (request.method == 'POST'):
        f = request.files['file1']
        f.save(os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(f.filename)))
        return "uploaded successfully"


if __name__ == "__main__":
    app.run(debug=True)

加载时出现此错误:JSON。解码器.JSONDecodeError:期望值:

或者有时当我添加一些东西时:JSON。decoder.JSONDecodeError:期望用双引号括起来的属性名称:

This is My ERROR:
    C:\Users\HP\anaconda3\envs\AutoML\python.exe "E:/Ineuron Internship/Automated 
    ML/AutoML/MAIN1.py"
    Traceback (most recent call last):
      File "E:/Ineuron Internship/Automated ML/AutoML/MAIN1.py", line 9, in <module>
        params = json.load(E)
      File "C:\Users\HP\anaconda3\envs\AutoML\lib\json\__init__.py", line 299, in load
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
      File "C:\Users\HP\anaconda3\envs\AutoML\lib\json\__init__.py", line 354, in loads
        return _default_decoder.decode(s)
      File "C:\Users\HP\anaconda3\envs\AutoML\lib\json\decoder.py", line 339, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "C:\Users\HP\anaconda3\envs\AutoML\lib\json\decoder.py", line 355, in raw_decode
        obj, end = self.scan_once(s, idx)
    json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 8 
    column 1 (char 103)
4

1 回答 1

3

据我所知,问题不在于您的代码,而在于您的 JSON 文件:

This is my JSON.config { {
  "params":
  {
    "upload_location":"E:\\Ineuron Internship\\Automated ML\\AutoML\\uploads"


  }, # The extra comma here means another key is meant to be present
}

要解决此问题,只需删除逗号:

This is my JSON.config { {
  "params":
  {
    "upload_location":"E:\\Ineuron Internship\\Automated ML\\AutoML\\uploads"


  }
}
于 2021-08-12T05:24:30.323 回答