python日记-使用蒙版marshal

蒙版顾名思义,就是把一些东西遮盖掉

如果中间有中文的话(包括注释)一定要在文件的最顶部添加

#coding=utf-8


首先在头部添加

import json
from flask.ext.restful import  fields,  marshal

首先给出一组数据,如:

data={'name':'Jack','address':'Guangdong','sex':'female'}

然后设定过滤规则(名字自己起)

resource_fileds={}
#将传入数据的name属性更名为XingMing,内容不变
resource_fileds['XingMing']=fields.String(attribute='name')
#直接传递Dizhi属性
resource_fileds['DiZhi']=fields.String
#添加一个属性Date
resource_fileds['Date']=fields.String('2016')

然后输出
print json.dumps(marshal(data,resource_fileds))

输出结果为

{"Date": "2016", "DiZhi": null, "XingMing": "Jack"}


蒙版还能嵌套和继承

在原基础上新建一个蒙版

another_fileds={}
another_fileds['person1']=fields.Nested(resource_fileds)
another_fileds['person2']=fields.Nested(resource_fileds)
传入数据

data1={'name':'mayun','address':'hangzhou','sex':'female'}
data2={'name':'mahuateng','address':'shenzhen','sex':'female'}
data3={'person1':data1,'person2':data2}

输出经新蒙版处理后数据

print json.dumps(marshal(data3,another_fileds))

输出结果为

{"person2": {"Date": "2016", "DiZhi": null, "XingMing": "mahuateng"}, "person1": {"Date": "2016", "DiZhi": null, "XingMing": "mayun"}}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值