django实现HttpResponse返回json数据为中文


Posted in Python onMarch 27, 2020

Python3读取写入json的中文乱码问题

之前我用django一般用JsonResponse来返回json数据格式

但是发现返回中文的时候会乱码

from django.http import JsonResponse
def test(request):
 result = {"result": 0, "msg": "执行成功"}
 return return JsonResponse(result)

这种方式返回简单,但是中文会乱码

现在改成用HttpResponse来返回,显示中文成功

from django.http import HttpResponse
import json
def test(request):
 result = {"result": 0, "msg": "执行成功"}
 #json返回为中文
 return HttpResponse(json.dumps(result,ensure_ascii=False),content_type="application/json,charset=utf-8")

补充知识:Django中的HttpResponse和JsonResponse

我们在编写一些接口函数的时候,经常需要给调用者返回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:是text/html,也就是字符串类型的返回,所以这段返回值并不是一个标准的json数据,是一个长得像json数据的字符串,当然可以通过工具直接转换为json,不过既然是一个json的接口,那么我们抛出的数据自然是json格式的最好,那如何抛出标准json格式的数据呢?

稍稍修改一丢丢代码,在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")

现在返回的就是application/json了;

那么Django提供了更方便的方法那就是JsonResponse,它内置帮我们封装了这个转换的操作,也就是说我们的接口抛json数据的话那么将HttpResponse替换为JsonResponse就OK了

1.首先先传dict数据:

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)

成功收到json数据;

2.接着再试试list数据:

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.

所以我们如果需要将非dict类型的数据进行JsonResponse传值,需要将safe参数设置为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)

此时成功接收到数据。

3.如果我们需要使用JsonResponse传中文

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

此时需要添加'json_dumps_params={‘ensure_ascii':False}',因为json序列化中文用的是ascii编码,所以传到前台的中文是ascii字符码,需要这一步转化为中文。

以上这篇django实现HttpResponse返回json数据为中文就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python MySQLdb Windows下安装教程及问题解决方法
May 09 Python
Python中的条件判断语句基础学习教程
Feb 07 Python
快速实现基于Python的微信聊天机器人示例代码
Mar 03 Python
python中 chr unichr ord函数的实例详解
Aug 06 Python
Python堆排序原理与实现方法详解
May 11 Python
用Python分析3天破10亿的《我不是药神》到底神在哪?
Jul 12 Python
VSCode中自动为Python文件添加头部注释
Nov 14 Python
Python遍历字典方式就实例详解
Dec 28 Python
使用Pyhton 分析酒店针孔摄像头
Mar 04 Python
Pycharm最常用的快捷键及使用技巧
Mar 05 Python
PyCharm最新激活码PyCharm2020.2.3有效
Nov 18 Python
使用pandas读取表格数据并进行单行数据拼接的详细教程
Mar 03 Python
python对XML文件的操作实现代码
Mar 27 #Python
Python Socketserver实现FTP文件上传下载代码实例
Mar 27 #Python
使用python从三个角度解决josephus问题的方法
Mar 27 #Python
解决django接口无法通过ip进行访问的问题
Mar 27 #Python
Django 实现将图片转为Base64,然后使用json传输
Mar 27 #Python
python实现简单坦克大战
Mar 27 #Python
Django实现从数据库中获取到的数据转换为dict
Mar 27 #Python
You might like
基于PHP服务端图片生成缩略图的方法详解
2013/06/20 PHP
php去除换行(回车换行)的三种方法
2014/03/26 PHP
php实现猴子选大王问题算法实例
2015/04/20 PHP
解决php的“It is not safe to rely on the system’s timezone settings”问题
2015/10/08 PHP
获取body标签的两种方法
2011/10/13 Javascript
jQuery $.get 的妙用 访问本地文本文件
2012/07/12 Javascript
append和appendTo的区别以及appendChild用法
2013/12/24 Javascript
jQuery如何使用自动触发事件trigger
2015/11/29 Javascript
基于Javascript实现倒计时功能
2016/02/22 Javascript
node.js cookie-parser之parser.js
2016/06/06 Javascript
jquery中的常见问题及快速解决方法小结
2016/06/14 Javascript
基于jQuery实现表格的查看修改删除
2016/08/01 Javascript
Bootstrap模态框水平垂直居中与增加拖拽功能
2016/11/09 Javascript
Vue2.0利用 v-model 实现组件props双向绑定的优美解决方案
2017/03/13 Javascript
bootstrap suggest下拉框使用详解
2017/04/10 Javascript
es6学习笔记之Async函数的使用示例
2017/05/11 Javascript
对Vue2 自定义全局指令Vue.directive和指令的生命周期介绍
2018/08/30 Javascript
jQuery移动端跑马灯抽奖特效升级版(抽奖概率固定)实现方法
2019/01/18 jQuery
Array.filter中如何正确使用Async
2020/11/04 Javascript
[49:27]LGD vs OG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
Python列表(list)常用操作方法小结
2015/02/02 Python
python实现中文输出的两种方法
2015/05/09 Python
Python统计文件中去重后uuid个数的方法
2015/07/30 Python
Python Socket实现简单TCP Server/client功能示例
2017/08/05 Python
python实现感知器算法详解
2017/12/19 Python
keras获得model中某一层的某一个Tensor的输出维度教程
2020/01/24 Python
OpenCV灰度化之后图片为绿色的解决
2020/12/01 Python
纽约手袋品牌:KARA
2018/03/18 全球购物
奢华时尚的创新平台:Baltini
2020/10/03 全球购物
个人公司授权委托书范本
2014/10/12 职场文书
单位作风建设自查报告
2014/10/23 职场文书
2015年教师节贺卡寄语
2015/03/24 职场文书
地心历险记观后感
2015/06/15 职场文书
Golang实现AES对称加密的过程详解
2021/05/20 Golang
PyTorch device与cuda.device用法
2022/04/03 Python
Mysql如何查看是否使用到索引
2022/12/24 MySQL