<pre name="code" class="python">初学python,分不太清楚json.dump和json,写段简单代码,测试一下。
代码如下:
#coding=utf-8
import json
dict_={'name': 'zxs', 'age':6, 5:3,'24':'25'}
print type(dict_), dict_
json_str=json.dumps(dict_)
print "json.dumps(dict) return:"
print type(json_str), json_str
json_loads=json.loads(json_str)
print "json.loads(str) return:"
print type(json_loads), json_loads
运行结果:
<img src="https://img-blog.csdn.net/20160531202552806?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
由运行结果可知:
json.dumps是将dict转化为str(格式为json);
json.loads是将字符串格式的json串,转化为dict.