今天就跟大家聊聊有關(guān)怎么在Django中返回json數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站是專業(yè)的騰沖網(wǎng)站建設(shè)公司,騰沖接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行騰沖網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!方法一:使用Python的JSON包
from django.shortcuts import HttpResponse import json def testjson(request): data={ 'patient_name': '張三', 'age': '25', 'patient_id': '19000347', '診斷': '上呼吸道感染', } return HttpResponse(json.dumps(data))
我們暫且把data看成是從數(shù)據(jù)庫取出來的數(shù)據(jù),使用瀏覽器訪問一下testjson
這不是亂碼,這是中文在內(nèi)存中的二進(jìn)制表現(xiàn)形式而已,使用JSON的轉(zhuǎn)換工具可以看到中文。
我們看一下Response Headers響應(yīng)頭,其中的Content-Type
是text/html
,我明明傳的是JSON啊,怎么會(huì)變成字符串類型了?這是因?yàn)槲覀儧]有告訴瀏覽器,我們要傳一個(gè)JSON數(shù)據(jù),那么,怎么告訴瀏覽器呢?
def testjson(request): data={ 'patient_name': '張三', 'age': '25', 'patient_id': '19000347', '診斷': '上呼吸道感染', } return HttpResponse(json.dumps(data), content_type='application/json')
再訪問網(wǎng)頁:
現(xiàn)在是傳輸JSON了,在Preview中可以正常顯示出來。
方法二:使用JsonResponse進(jìn)行傳輸
def testjson(request): data={ 'patient_name': '張三', 'age': '25', 'patient_id': '19000347', '診斷': '上呼吸道感染', } return JsonResponse(data)
訪問網(wǎng)頁:
JsonResponse的源碼
class JsonResponse(HttpResponse): """ An HTTP response class that consumes data to be serialized to JSON. :param data: Data to be dumped into json. By default only ``dict`` objects are allowed to be passed due to a security flaw before EcmaScript 5. See the ``safe`` parameter for more information. :param encoder: Should be a json encoder class. Defaults to ``django.core.serializers.json.DjangoJSONEncoder``. :param safe: Controls if only ``dict`` objects may be serialized. Defaults to ``True``. :param json_dumps_params: A dictionary of kwargs passed to json.dumps(). """ def __init__(self, data, encoder=DjangoJSONEncoder, safe=True, json_dumps_params=None, **kwargs): if safe and not isinstance(data, dict): raise TypeError( 'In order to allow non-dict objects to be serialized set the ' 'safe parameter to False.' ) if json_dumps_params is None: json_dumps_params = {} kwargs.setdefault('content_type', 'application/json') data = json.dumps(data, cls=encoder, **json_dumps_params) super().__init__(content=data, **kwargs)
其內(nèi)部也是通過json.dumps來把數(shù)據(jù)轉(zhuǎn)換為JSON的,其還可以轉(zhuǎn)換為list類型。我們?cè)賮砀囊幌聇estjson
def testjson(request): listdata = ["張三", "25", "19000347", "上呼吸道感染"] return JsonResponse(listdata)
程序報(bào)錯(cuò)了
報(bào)錯(cuò)為:In order to allow non-dict objects to be serialized set the safe parameter to False
,它的意思是轉(zhuǎn)換為一個(gè)非字典的類型時(shí),safe參數(shù)要設(shè)置為False,還記得上面JsonResponse的原碼嗎?其中就有
代碼修改為:
def testjson(request): listdata = ["張三", "25", "19000347", "上呼吸道感染"] return JsonResponse(listdata, safe=False)
看完上述內(nèi)容,你們對(duì)怎么在Django中返回json數(shù)據(jù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前文章:怎么在Django中返回json數(shù)據(jù)-創(chuàng)新互聯(lián)
URL標(biāo)題:http://aaarwkj.com/article34/cdpise.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、網(wǎng)站維護(hù)、用戶體驗(yàn)、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容