Django可以很方便地將python對象轉(zhuǎn)換為JSON格式。以下是一個示例:
import json data = {"name": "Jack", "age": 30, "city": "New York"} json_data = json.dumps(data) print(json_data)
運行上述代碼將輸出以下結(jié)果:
{"name": "Jack", "age": 30, "city": "New York"}
通過從瀏覽器中接收POST請求的方式來生成JSON數(shù)據(jù):
from django.http import JsonResponse def myview(request): if request.method == "POST": data = {'result': 'success', 'message': 'Your request has been received.'} return JsonResponse(data) else: data = {'result': 'error', 'message': 'Invalid request.'} return JsonResponse(data)
POST請求將返回JSON數(shù)據(jù):
{"result": "success", "message": "Your request has been received."}
上一篇vue中vantui使用
下一篇python 生成注冊碼