python json.loads json.dumps的区别

json.loads() 是将字符串传化为字典 
json.dumps () 是将字典转化为字符串
>>> dict = "{8:'bye', 'you':'coder'}" #假字典 fake
>>> print type(dict)
<type 'str'>
>>> print dict
{8:'bye', 'you':'coder'} # 哟呵,输出

>>> dict_real = {8:'bye', 'you':'coder'} # 真字典 real
>>> print dict_real
{8: 'bye', 'you': 'coder'} # 这是真字典的输出。
# 然后我们来获取键 'you'的值
# 真字典情况
>>> print dict_real['you']
coder # 没有问题哦,

# 看看假字典的真面目啦。。。。。。
>>> print dict['you']
Traceback (most recent call last): # 错误咯,这就是原因啦。
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not str

转载于:https://www.cnblogs.com/muahao/p/11076757.html

猜你喜欢

转载自blog.csdn.net/weixin_34362991/article/details/93632346