成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

怎么在django中使用HttpResponse返回json數(shù)據(jù)-創(chuàng)新互聯(lián)

怎么在django中使用HttpResponse返回json數(shù)據(jù)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)公司憑借專業(yè)的設(shè)計(jì)團(tuán)隊(duì)扎實(shí)的技術(shù)支持、優(yōu)質(zhì)高效的服務(wù)意識(shí)和豐厚的資源優(yōu)勢(shì),提供專業(yè)的網(wǎng)站策劃、成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站優(yōu)化、軟件開(kāi)發(fā)、網(wǎng)站改版等服務(wù),在成都十載的網(wǎng)站建設(shè)設(shè)計(jì)經(jīng)驗(yàn),為成都千余家中小型企業(yè)策劃設(shè)計(jì)了網(wǎng)站。
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)

手機(jī)網(wǎng)站建設(shè)
常山县| 平阳县| 徐闻县| 鄂托克旗| 冷水江市| 原阳县| 南陵县| 昆山市| 建湖县| 伊春市| 大渡口区| 安阳市| 盖州市| 双流县| 拜泉县| 清涧县| 文成县| 铅山县| 琼中| 福建省| 柞水县| 黎城县| 金溪县| 青神县| 独山县| 工布江达县| 万安县| 武冈市| 阳西县| 淳化县| 云浮市| 田林县| 浮梁县| 同江市| 文化| 祁阳县| 建宁县| 涞源县| 刚察县| 揭阳市| 九江县|