json模塊

編碼:json.dumps() 把python對象編碼轉換成json字符串python

解碼:json.loads()把json格式字符串解碼轉換成python對象json

  1. #!/usr/bin/python3  
  2. from json import *  
  3. if __name__=="__main__":     
  4.    d={}  
  5.    d['a'] =1  
  6.    d['b']=2  
  7.    d[3]='c'  
  8.    d[4]=['k','k1']    
  9.    #將Python dict類型轉換成標準Json字符串  
  10.    k=JSONEncoder().encode(d)  
  11.    print(type(k))  
  12.    print(k)
  13. <type 'str'>
  14. {"a": 1, "3": "c", "b": 2, "4": ["k", "k1"]}編碼

##################################################spa

  1.    #將json字符串轉換成Python dict類型  
  2.    json_str='{"a":1,"b":2,"3":"c","4":["k","k1"]}'  
  3.    d=JSONDecoder().decode(json_str)  
  4.    print(type(d))  
  5.    print(d)  

<type 'str'>
<type 'dict'>
{u'a': 1, u'3': u'c', u'b': 2, u'4': [u'k', u'k1']}code

相關文章
相關標籤/搜索