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

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

創(chuàng)新互聯(lián)公司憑借專業(yè)的設(shè)計團隊扎實的技術(shù)支持、優(yōu)質(zhì)高效的服務(wù)意識和豐厚的資源優(yōu)勢,提供專業(yè)的網(wǎng)站策劃、成都網(wǎng)站設(shè)計、做網(wǎng)站、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務(wù),在成都十載的網(wǎng)站建設(shè)設(shè)計經(jīng)驗,為成都千余家中小型企業(yè)策劃設(shè)計了網(wǎng)站。
from django.http import JsonResponse
def test(request):
 result = {"result": 0, "msg": "執(zhí)行成功"}
 return return JsonResponse(result)

這種方式返回簡單,但是中文會亂碼

現(xiàn)在改成用HttpResponse來返回,顯示中文成功

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")

補充知識:Django中的HttpResponse和JsonResponse

我們在編寫一些接口函數(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))

這里前臺的返回信息中,返回的Content-Type:是text/html,也就是字符串類型的返回,所以這段返回值并不是一個標準的json數(shù)據(jù),是一個長得像json數(shù)據(jù)的字符串,當然可以通過工具直接轉(zhuǎn)換為json,不過既然是一個json的接口,那么我們拋出的數(shù)據(jù)自然是json格式的最好,那如何拋出標準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)置幫我們封裝了這個轉(zhuǎn)換的操作,也就是說我們的接口拋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)

此時查看輸出,卻報錯了:

In order to allow non-dict objects to be serialized set the safe parameter to False.

所以我們?nèi)绻枰獙⒎莇ict類型的數(shù)據(jù)進行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ù)據(jù)。

3.如果我們需要使用JsonResponse傳中文

def func(request):
 data={'姓名':'釋明空'}
 return JsonResponse(data,json_dumps_params={'ensure_ascii':False})

此時需要添加'json_dumps_params={‘ensure_ascii':False}',因為json序列化中文用的是ascii編碼,所以傳到前臺的中文是ascii字符碼,需要這一步轉(zhuǎn)化為中文。

關(guān)于怎么在django中使用HttpResponse返回json數(shù)據(jù)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

本文標題:怎么在django中使用HttpResponse返回json數(shù)據(jù)-創(chuàng)新互聯(lián)
分享路徑:http://www.muchs.cn/article32/dpcipc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導航網(wǎng)站導航外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈定制網(wǎng)站、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)