新聞中心
這篇文章主要為大家詳細(xì)介紹了怎么中python中對Json與object進(jìn)行轉(zhuǎn)換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,發(fā)現(xiàn)的小伙伴們可以參考一下:
python是什么意思
Python是一種跨平臺(tái)的、具有解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計(jì)是用于編寫自動(dòng)化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xiàng)目。
python提供了json包來進(jìn)行json處理,json與python中數(shù)據(jù)類型對應(yīng)關(guān)系如下:
一個(gè)python object無法直接與json轉(zhuǎn)化,只能先將對象轉(zhuǎn)化成dictionary,再轉(zhuǎn)化成json;對json,也只能先轉(zhuǎn)換成dictionary,再轉(zhuǎn)化成object,通過實(shí)踐,源碼如下:
import json class user: def __init__(self, name, pwd): self.name = name self.pwd = pwd def __str__(self): return 'user(' + self.name + ',' + self.pwd + ')' #重寫JSONEncoder的default方法,object轉(zhuǎn)換成dict class userEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, user): return { 'name': o.name, 'pwd': o.pwd } return json.JSONEncoder.default(o) #重寫JSONDecoder的decode方法,dict轉(zhuǎn)換成object class userDecode(json.JSONDecoder): def decode(self, s): dic = super().decode(s) return user(dic['name'], dic['pwd']) #重寫JSONDecoder的__init__方法,dict轉(zhuǎn)換成object class userDecode2(json.JSONDecoder): def __init__(self): json.JSONDecoder.__init__(self, object_hook=dic2objhook) # 對象轉(zhuǎn)換成dict def obj2dict(obj): if (isinstance(obj, user)): return { 'name': obj.name, 'pwd': obj.pwd } else: return obj # dict轉(zhuǎn)換為對象 def dic2objhook(dic): if isinstance(dic, dict): return user(dic['name'], dic['pwd']) return dic # 第一種方式,直接把對象先轉(zhuǎn)換成dict u = user('smith', '123456') uobj = json.dumps(obj2dict(u)) print('uobj: ', uobj) #第二種方式,利用json.dumps的關(guān)鍵字參數(shù)default u = user('smith', '123456') uobj2 = json.dumps(u, default=obj2dict) print('uobj2: ', uobj) #第三種方式,定義json的encode和decode子類,使用json.dumps的cls默認(rèn)參數(shù) user_encode_str = json.dumps(u, cls=userEncoder) print('user2json: ', user_encode_str) #json轉(zhuǎn)換為object u2 = json.loads(user_encode_str, cls=userDecode) print('json2user: ', u2) #另一種json轉(zhuǎn)換成object的方式 u3 = json.loads(user_encode_str, cls=userDecode2) print('json2user2: ', u3)
輸出結(jié)果如下:
C:\python\python.exe C:/Users/Administrator/PycharmProjects/pytest/com/guo/myjson.py uobj: {"name": "smith", "pwd": "123456"} uobj2: {"name": "smith", "pwd": "123456"} user2json: {"name": "smith", "pwd": "123456"} json2user: user(smith,123456) json2user2: user(smith,123456) Process finished with exit code 0
以上就是創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司小編為大家收集整理的怎么中python中對Json與object進(jìn)行轉(zhuǎn)換,如何覺得創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司網(wǎng)站推薦給身邊好友。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計(jì)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前標(biāo)題:怎么中python中對Json與object進(jìn)行轉(zhuǎn)換-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://fisionsoft.com.cn/article/desics.html