标签:接口交互 color 字符 restful 后台 调用 code str arch
search_info = {‘id‘:132,‘user_role‘:3}
print type(search_info)      #输出 <type ‘dict‘>
#转为string用dumps
print type(json.dumps(search_info))  #输出 <type ‘str‘>
#string转 dict用 loads()
print type(json.loads(json.dumps(search_info)))   #输出  <type ‘dict‘>
如果前后台通过接口交互时,返回给前台json格式数据时可以使用jsonify
#返回给前台json格式数据
return jsonify({‘id‘:132,‘user_role‘:3})
如果前台调用A后台接口,A后台接口调用B后台接口,则A后台接口得到B后台的数据返回给前台时,用如下方法:
r = requests.get(www.xxx.com/restful, params={‘user_id‘:1})
#返回的字符串数据先转为dict,再通过json格式传给前台
return jsonify(json.loads(r.text))
python中json.loads,dumps,jsonify使用
标签:接口交互 color 字符 restful 后台 调用 code str arch
原文地址:http://www.cnblogs.com/rgcLOVEyaya/p/RGC_LOVE_YAYA_361_1days.html