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
相關文章
相關標籤/搜索