欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

django 寫json數(shù)據(jù)

傅智翔1年前9瀏覽0評論

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."}