python flask解析json数据不完整的解决方法
å½ä½¿ç¨Pythonçflaskæ¡æ¶æ¥å¼åç½ç«åå°ï¼è§£æå端Postæ¥çæ°æ®ï¼é常é½ä¼ä½¿ç¨request.formæ¥è·ååç«¯ä¼ è¿æ¥çæ°æ®ï¼ä½æ¯å¦æä¼ è¿æ¥çæ°æ®æ¯è¾å¤æï¼å¶ä¸å³arrayï¼èä¸arrayçåç´ ä¸æ¯å个çæ°åæèå符串çæ¶åï¼å°±ä¼åºç°è§£æä¸å°æ°æ®çæåµï¼æ¯å¦ä½¿ç¨ä¸é¢çjs代ç åpython flaskä¼ éæ°æ®
$.ajax({ ãã"url":"/test", ãã"method":"post", ãã"data":{ ãããããã"test":[ ãããããããã{"test_dict":"1"}, ãããããããã{"test_dict":"2"}, ãããããããã{"test_dict":"3"}, ãããããããã] ãããããã} ãã} )
å½æ们使ç¨flaskçrequest.formè·åå端çæ°æ®æ¶ï¼åç°è·åå°çæ°æ®æ¯è¿æ ·çï¼
ImmutableMultiDict([('test', 'test_dict'), ('test', 'test_dict'), ('test', 'test_dict')])
ï¼ï¼ï¼æçPostæ°æ®å¢ï¼ç»æpostå°åªéå»äºï¼ï¼ï¼
è¿éæå°±å»ç½ä¸æ¥è§£å³åæ³ï¼ä½æ¯ç½ä¸åªäºå ä¹ä½¿ç¨reqeust.form.getlist()æ¹æ³å¥½åé½å¯¹ææ æï¼ä½æ¯åæ¾ä¸å°å¶ä»ç解å³æ¹æ¡ï¼æä¹åï¼
è§èä¸ä¸èªå·±ç请æ±ï¼å¨å端请æ±çæ¶å设置ä¸ä¸ªJsonç请æ±å¤´ï¼å¨flaskæ¡æ¶éç´æ¥ä½¿ç¨json.loads()æ¹æ³è§£æreqeust.get_data(as_text=True)ï¼å°±å¯ä»¥è§£æå°å®æ´çpoståæ°äºï¼
å端ï¼
$.ajax({ ãã"url":"/test", ãã"method":"post", ãã"headers":{"Content-Type": "application/json;charset=utf-8"},//è¿ä¸å¥å¾éè¦ï¼ï¼ï¼ ãã"data":{ ãããã"test":[ ãããããããã{"test_dict":"1"}, ãããããããã{"test_dict":"2"}, ãããããããã{"test_dict":"3"}, ãããããã] ãããã} ãã} ãã)
python代ç ï¼
@app.route("/test",methods=["GET","POST"]) def test(): print(json.loads(request.get_data(as_text=True))) return ""
ç¶åççåå°æå°çä¿¡æ¯ï¼
* Serving Flask app "test_flask.py" * Environment: development * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) {'test': [{'test_dict': '1'}, {'test_dict': '2'}, {'test_dict': '3'}]} 127.0.0.1 - - [25/May/2019 22:43:08] "POST /test HTTP/1.1" 200 -
é®é¢è§£å³ï¼å¯ä»¥è§£æå°å®æ´çjsonæ°æ®å¦ï¼
以ä¸å°±æ¯æ¬æçå¨é¨å容ï¼å¸æ对大家çå¦ä¹ ææ帮å©ï¼ä¹å¸æ大家å¤å¤æ¯æèæ¬ä¹å®¶ã