對(duì)python中Json與object轉(zhuǎn)化的方法詳解
python提供了json包來進(jìn)行json處理,json與python中數(shù)據(jù)類型對(duì)應(yīng)關(guān)系如下:
一個(gè)python object無法直接與json轉(zhuǎn)化,只能先將對(duì)象轉(zhuǎn)化成dictionary,再轉(zhuǎn)化成json;對(duì)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) # 對(duì)象轉(zhuǎn)換成dict def obj2dict(obj): if (isinstance(obj, user)): return { 'name': obj.name, 'pwd': obj.pwd } else: return obj # dict轉(zhuǎn)換為對(duì)象 def dic2objhook(dic): if isinstance(dic, dict): return user(dic['name'], dic['pwd']) return dic # 第一種方式,直接把對(duì)象先轉(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
以上這篇對(duì)python中Json與object轉(zhuǎn)化的方法詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python標(biāo)準(zhǔn)庫(kù)json模塊和pickle模塊使用詳解
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)json模塊和pickle模塊使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03pymysql實(shí)現(xiàn)增刪改查的操作指南(python)
python中可以使用pymysql來MySQL數(shù)據(jù)庫(kù)的連接,并實(shí)現(xiàn)數(shù)據(jù)庫(kù)的各種操作,這篇文章主要給大家介紹了關(guān)于pymsql實(shí)現(xiàn)增刪改查的相關(guān)資料,需要的朋友可以參考下2021-05-05用Python獲取攝像頭并實(shí)時(shí)控制人臉的實(shí)現(xiàn)示例
這篇文章主要介紹了用Python獲取攝像頭并實(shí)時(shí)控制人臉的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python pyautogui手動(dòng)活動(dòng)(模擬鼠標(biāo)鍵盤)自動(dòng)化庫(kù)使用
這篇文章主要為大家介紹了python pyautogui手動(dòng)活動(dòng)(模擬鼠標(biāo)鍵盤)自動(dòng)化庫(kù)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01如何使用matplotlib讓你的數(shù)據(jù)更加生動(dòng)
數(shù)據(jù)可視化用于以更直接的表示方式顯示數(shù)據(jù),并且更易于理解,下面這篇文章主要給大家介紹了關(guān)于如何使用matplotlib讓你的數(shù)據(jù)更加生動(dòng)的相關(guān)資料,需要的朋友可以參考下2021-11-11python基于OpenCV模塊實(shí)現(xiàn)視頻流數(shù)據(jù)切割為圖像幀數(shù)據(jù)(流程分析)
這篇文章主要介紹了python基于OpenCV模塊實(shí)現(xiàn)視頻流數(shù)據(jù)切割為圖像幀數(shù)據(jù),這里今天主要是實(shí)踐一下視頻流數(shù)據(jù)的預(yù)處理工作,需要的朋友可以參考下2022-05-05