我的flask服務器接收一個json文件。
@app.route('/process', methods=['POST'])
def trigger_processing():
sent_files = request.files
json_file = sent_files['file']
print(json_file)
data = json.load(json_file)
print(data)
return make_response(json.dumps({'fileid': "ok"}), 200)
打印json_file
給了我:
<FileStorage: 'test.json' ('application/octet-stream')>
127.0.0.1 - - [31/May/2021 23:29:55] "POST /process HTTP/1.1" 200 -
但是,當我嘗試將json轉換為dictionary時,程序失敗了。
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 11 column 5 (char 213)
我想把json轉換成字典。理想情況下,我還希望將收到的json文件保存在服務器的文件系統中。
它失敗了,因為
sent_files['file']
是FileStorage對象類型,您需要讀取它并將讀取的字節解碼為字符串。然后可以將其作為json加載。