django實(shí)現(xiàn)HttpResponse返回json數(shù)據(jù)為中文
Python3讀取寫入json的中文亂碼問(wèn)題
之前我用django一般用JsonResponse來(lái)返回json數(shù)據(jù)格式
但是發(fā)現(xiàn)返回中文的時(shí)候會(huì)亂碼
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)化為中文。
以上這篇django實(shí)現(xiàn)HttpResponse返回json數(shù)據(jù)為中文就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?數(shù)據(jù)類型中的字符串和數(shù)字
這篇文章主要介紹了Python?數(shù)據(jù)類型中的字符串和數(shù)字,Python3中有六個(gè)標(biāo)準(zhǔn)的數(shù)據(jù)類型,Number、String、List、Tuple、Set、Dictionary,加先來(lái)我們就來(lái)看看這幾種數(shù)據(jù)類型的具體相關(guān)介紹,需要的小伙伴可以參考一下2022-02-02詳解Python中while無(wú)限迭代循環(huán)方法
Python 有 while 語(yǔ)句和 for 語(yǔ)句作為循環(huán)處理。雖然 for 語(yǔ)句具有一定數(shù)量的進(jìn)程,但 while 語(yǔ)句是直到滿足條件類型的循環(huán)進(jìn)程。本文將詳解while無(wú)限迭代循環(huán)方法,需要的可以了解一下2022-04-04Python使用PEfile模塊實(shí)現(xiàn)分析PE文件
PeFile模塊是Python中一個(gè)強(qiáng)大的便攜式第三方PE格式分析工具,用于解析和處理Windows可執(zhí)行文件,本文主要就來(lái)講講如何使用PEfile模塊實(shí)現(xiàn)分析PE文件,需要的可以參考下2023-08-08Django處理文件上傳File Uploads的實(shí)例
今天小編就為大家分享一篇Django處理文件上傳File Uploads的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05數(shù)據(jù)可視化Pyecharts的實(shí)際使用方式
這篇文章主要介紹了數(shù)據(jù)可視化Pyecharts的實(shí)際使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04python庫(kù)Tsmoothie模塊數(shù)據(jù)平滑化異常點(diǎn)抓取
這篇文章主要為大家介紹了python庫(kù)Tsmoothie模塊數(shù)據(jù)平滑化技術(shù)實(shí)現(xiàn)異常點(diǎn)抓取,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06