运维开发笔记整理-JsonResponse对象

          运维开发笔记整理-JsonResponse对象 

                                   作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.使用HttpResponse发送json格式的数据

1>.HttpResponse默认使用的是文本格式(text/html)

#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/

from django.http import HttpResponse
import json
def index(request):
    data = {
        "name":"yinzhengjie",
        "age":"26",
    }

    return HttpResponse(json.dumps(data))

2>.使用HttpResponse传递json格式的数据

#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/

from django.http import HttpResponse
import json
def index(request):
    data = {
        "name":"yinzhengjie",
        "age":"26",
    }

    return HttpResponse(json.dumps(data),content_type="application/json")

 

 

二.使用JsonResponse传递json格式的数据

1>.JsonResponse是继承HttpResponse

   注意,JsonResponse其实已经帮我们做了一些调优工作,如下图:

 

2>.使用JsonResponse传递json格式的数据

#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/

from django.http import JsonResponse

def index(request):
    data = {
        "name":"yinzhengjie",
        "age":"26",
    }
    return JsonResponse(data)

 

转载于:https://www.cnblogs.com/yinzhengjie/p/10245276.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值