Python JSON常用編解碼方法代碼實(shí)例
概念
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式,易于人閱讀和編寫(xiě)。在日常的工作中,應(yīng)用范圍極其廣泛。這里就介紹python下它的兩種編解碼方法:
使用json函數(shù)
使用 JSON 函數(shù)需要導(dǎo)入 json 庫(kù):import json。函數(shù)含義:
源碼解析:
# coding= utf-8#!/usr/bin/pythonimport jsonimport sys data = {'username':'測(cè)試','age':16}#jsondata = json.dumps(data,ensure_ascii=False)jsondata = json.dumps(data)print('data convert to json')print type(json)text = json.loads(jsondata)print('json convert to data')print text['username']print text['age']
使用第三方庫(kù):Demjson
Demjson 是 python 的第三方模塊庫(kù),可用于編碼和解碼 JSON 數(shù)據(jù),包含了 JSONLint 的格式化及校驗(yàn)功能。
函數(shù)定義:
源碼解析:
#!/usr/bin/pythonimport demjsondata = [ { ’a’ : 1, ’b’ : 2, ’c’ : 3, ’d’ : 4, ’e’ : 5 } ] json = demjson.encode(data)print jsontext = demjson.decode(json)print text
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Java之JSP教程九大內(nèi)置對(duì)象詳解(中篇)2. CSS自定義滾動(dòng)條樣式案例詳解3. Python實(shí)現(xiàn)查找數(shù)據(jù)庫(kù)最接近的數(shù)據(jù)4. python鏈表類(lèi)中獲取元素實(shí)例方法5. python中if嵌套命令實(shí)例講解6. 使用css實(shí)現(xiàn)全兼容tooltip提示框7. python 批量下載bilibili視頻的gui程序8. python b站視頻下載的五種版本9. ASP基礎(chǔ)入門(mén)第三篇(ASP腳本基礎(chǔ))10. 詳解CSS不定寬溢出文本適配滾動(dòng)
