亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

django實(shí)現(xiàn)HttpResponse返回json數(shù)據(jù)為中文

瀏覽:68日期:2024-05-28 15:51:59

Python3讀取寫入json的中文亂碼問題

之前我用django一般用JsonResponse來返回json數(shù)據(jù)格式

但是發(fā)現(xiàn)返回中文的時(shí)候會(huì)亂碼

from django.http import JsonResponsedef test(request): result = {'result': 0, 'msg': '執(zhí)行成功'} return return JsonResponse(result)

這種方式返回簡(jiǎn)單,但是中文會(huì)亂碼

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

from django.http import HttpResponseimport jsondef 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 renderfrom django.http import HttpResponse,JsonResponseimport 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)然可以通過工具直接轉(zhuǎn)換為json,不過既然是一個(gè)json的接口,那么我們拋出的數(shù)據(jù)自然是json格式的最好,那如何拋出標(biāo)準(zhǔn)json格式的數(shù)據(jù)呢?

稍稍修改一丟丟代碼,在HttpResponse中添加content_type類型為json的屬性

from django.shortcuts import renderfrom django.http import HttpResponse,JsonResponseimport 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)換的操作,也就是說我們的接口拋json數(shù)據(jù)的話那么將HttpResponse替換為JsonResponse就OK了

1.首先先傳dict數(shù)據(jù):

from django.shortcuts import renderfrom 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 renderfrom 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 renderfrom 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è)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 中文 字幕 高清 在线 | 久久影院一区二区三区 | 欧美日韩乱码毛片免费观看 | 99国产欧美久久精品 | 永久免费毛片 | 精品自拍视频在线观看 | 在线观看成年美女黄网色视频 | 97国产精品欧美一区二区三区 | 全免费a级毛片免费毛视频 全免费a级毛片免费看 | 久久成人精品 | 中文字幕不卡一区 二区三区 | 国产在线午夜 | 欧美三级成版人版在线观看 | 黄色免费的视频 | 亚洲xx在线 | 三级毛片三级毛片 | 麻豆成人传媒一区二区 | 国产第一草草影院 | 二区三区不卡不卡视频 | 91精品视频在线观看免费 | 在线观看黄网视频免费播放 | 在线视频观看免费视频18 | 99久久中文字幕伊人情人 | 在线亚洲一区二区 | 美女mm翘臀后进式动态图 | 国产精品久久毛片 | 国产一级做a爱免费视频 | 一区二区三区四区日韩 | 国产精品网站 夜色 | 97在线视频观看 | 黄色一级片免费网站 | a毛片在线播放 | wwwwxxxx免费| 欧美破处视频在线 | 精品日韩欧美一区二区三区在线播放 | 日韩精品一区二区三区 在线观看 | 天堂tv亚洲tv日本tv不卡 | 九九视频九九热 | 欧美毛片精品一区 | 亚洲一区无码中文字幕 | 国内精品久久久久影院嫩草 |