蒙版顾名思义,就是把一些东西遮盖掉
如果中间有中文的话(包括注释)一定要在文件的最顶部添加
#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"}}