怎么在django中使用HttpResponse返回json數(shù)據(jù)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
from django.http import JsonResponse def test(request): result = {"result": 0, "msg": "執(zhí)行成功"} return return JsonResponse(result)
這種方式返回簡(jiǎn)單,但是中文會(huì)亂碼
現(xiàn)在改成用HttpResponse來(lái)返回,顯示中文成功
from django.http import HttpResponse import json def test(request): result = {"result": 0, "msg": "執(zhí)行成功"} #json返回為中文 return HttpResponse(json.dumps(result,ensure_ascii=False),content_type="application/json,charset=utf-8")
補(bǔ)充知識(shí):Django中的HttpResponse和JsonResponse
我們?cè)诰帉懸恍┙涌诤瘮?shù)的時(shí)候,經(jīng)常需要給調(diào)用者返回json格式的數(shù)據(jù),那么如何返回可直接解析的數(shù)據(jù)呢?
首先第一種方式:
from django.shortcuts import render from django.http import HttpResponse,JsonResponse import json # Create your views here. def index(request): data={ 'name':'zhangsan', 'age':18, } return HttpResponse(json.dumps(data))
這里前臺(tái)的返回信息中,返回的Content-Type:是text/html,也就是字符串類型的返回,所以這段返回值并不是一個(gè)標(biāo)準(zhǔn)的json數(shù)據(jù),是一個(gè)長(zhǎng)得像json數(shù)據(jù)的字符串,當(dāng)然可以通過(guò)工具直接轉(zhuǎn)換為json,不過(guò)既然是一個(gè)json的接口,那么我們拋出的數(shù)據(jù)自然是json格式的最好,那如何拋出標(biāo)準(zhǔn)json格式的數(shù)據(jù)呢?
稍稍修改一丟丟代碼,在HttpResponse中添加content_type類型為json的屬性
from django.shortcuts import render from django.http import HttpResponse,JsonResponse import json # Create your views here. def index(request): data={ 'name':'zhangsan', 'age':18, } return HttpResponse(json.dumps(data),content_type="application/json")
現(xiàn)在返回的就是application/json了;
那么Django提供了更方便的方法那就是JsonResponse,它內(nèi)置幫我們封裝了這個(gè)轉(zhuǎn)換的操作,也就是說(shuō)我們的接口拋json數(shù)據(jù)的話那么將HttpResponse替換為JsonResponse就OK了
1.首先先傳dict數(shù)據(jù):
from django.shortcuts import render from django.http import HttpResponse,JsonResponse # Create your views here. def index(request): data={ 'name':'zhangsan', 'age':18, } return JsonResponse(data)
成功收到j(luò)son數(shù)據(jù);
2.接著再試試list數(shù)據(jù):
from django.shortcuts import render from django.http import HttpResponse,JsonResponse # Create your views here. def index(request): listdata=[1,2,3,4,5] return JsonResponse(listdata)
此時(shí)查看輸出,卻報(bào)錯(cuò)了:
In order to allow non-dict objects to be serialized set the safe parameter to False.
所以我們?nèi)绻枰獙⒎莇ict類型的數(shù)據(jù)進(jìn)行JsonResponse傳值,需要將safe參數(shù)設(shè)置為False
from django.shortcuts import render from django.http import HttpResponse,JsonResponse # Create your views here. def index(request): listdata=[1,2,3,4,5] return JsonResponse(listdata,safe=False)
此時(shí)成功接收到數(shù)據(jù)。
3.如果我們需要使用JsonResponse傳中文
def func(request): data={'姓名':'釋明空'} return JsonResponse(data,json_dumps_params={'ensure_ascii':False})
此時(shí)需要添加'json_dumps_params={‘ensure_ascii':False}',因?yàn)閖son序列化中文用的是ascii編碼,所以傳到前臺(tái)的中文是ascii字符碼,需要這一步轉(zhuǎn)化為中文。
關(guān)于怎么在django中使用HttpResponse返回json數(shù)據(jù)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
本文標(biāo)題:怎么在django中使用HttpResponse返回json數(shù)據(jù)-創(chuàng)新互聯(lián)
分享路徑:http://jinyejixie.com/article32/dpcipc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、定制網(wǎng)站、App開(kāi)發(fā)
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容