Python之dict(或?qū)ο?與json之間的互相轉(zhuǎn)化實例
在Python語言中,json數(shù)據(jù)與dict字典以及對象之間的轉(zhuǎn)化,是必不可少的操作。
在Python中自帶json庫。通過import json導(dǎo)入。
在json模塊有2個方法,
loads():將json數(shù)據(jù)轉(zhuǎn)化成dict數(shù)據(jù)
dumps():將dict數(shù)據(jù)轉(zhuǎn)化成json數(shù)據(jù)
load():讀取json文件數(shù)據(jù),轉(zhuǎn)成dict數(shù)據(jù)
dump():將dict數(shù)據(jù)轉(zhuǎn)化成json數(shù)據(jù)后寫入json文件
下面是具體的示例:
dict字典轉(zhuǎn)json數(shù)據(jù)
import json def dict_to_json(): dict = {} dict['name'] = 'many' dict['age'] = 10 dict['sex'] = 'male' print(dict) # 輸出:{'name': 'many', 'age': 10, 'sex': 'male'} j = json.dumps(dict) print(j) # 輸出:{"name": "many", "age": 10, "sex": "male"} if __name__ == '__main__': dict_to_json()
對象轉(zhuǎn)json數(shù)據(jù)
import json def obj_to_json(): stu = Student('007', '007', 28, 'male', '13000000000', '123@qq.com') print(type(stu)) # <class 'json_test.student.Student'> stu = stu.__dict__ # 將對象轉(zhuǎn)成dict字典 print(type(stu)) # <class 'dict'> print(stu) # {'id': '007', 'name': '007', 'age': 28, 'sex': 'male', 'phone': '13000000000', 'email': '123@qq.com'} j = json.dumps(obj=stu) print(j) # {"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "123@qq.com"} if __name__ == '__main__': obj_to_json()
json數(shù)據(jù)轉(zhuǎn)成dict字典
import json def json_to_dict(): j = '{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "123@qq.com"}' dict = json.loads(s=j) print(dict) # {'id': '007', 'name': '007', 'age': 28, 'sex': 'male', 'phone': '13000000000', 'email': '123@qq.com'} if __name__ == '__main__': json_to_dict()
json數(shù)據(jù)轉(zhuǎn)成對象
import json def json_to_obj(): j = '{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "123@qq.com"}' dict = json.loads(s=j) stu = Student() stu.__dict__ = dict print('id: ' + stu.id + ' name: ' + stu.name + ' age: ' + str(stu.age) + ' sex: ' + str( stu.sex) + ' phone: ' + stu.phone + ' email: ' + stu.email) # id: 007 name: 007 age: 28 sex: male phone: 13000000000 email: 123@qq.com if __name__ == '__main__': json_to_obj()
json的load()與dump()方法的使用
dump()方法的使用
import json def dict_to_json_write_file(): dict = {} dict['name'] = 'many' dict['age'] = 10 dict['sex'] = 'male' print(dict) # {'name': 'many', 'age': 10, 'sex': 'male'} with open('1.json', 'w') as f: json.dump(dict, f) # 會在目錄下生成一個1.json的文件,文件內(nèi)容是dict數(shù)據(jù)轉(zhuǎn)成的json數(shù)據(jù) if __name__ == '__main__': dict_to_json_write_file()
load()的使用
import json def json_file_to_dict(): with open('1.json', 'r') as f: dict = json.load(fp=f) print(dict) # {'name': 'many', 'age': 10, 'sex': 'male'} if __name__ == '__main__': json_file_to_dict()
以上這篇Python之dict(或?qū)ο?與json之間的互相轉(zhuǎn)化實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 在python中利用dict轉(zhuǎn)json按輸入順序輸出內(nèi)容方式
- python 實現(xiàn)dict轉(zhuǎn)json并保存文件
- Python XML轉(zhuǎn)Json之XML2Dict的使用方法
- 對python中dict和json的區(qū)別詳解
- 詳解python中的json和字典dict
- Python 提取dict轉(zhuǎn)換為xml/json/table并輸出的實現(xiàn)代碼
- python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實例
- python3 json數(shù)據(jù)格式的轉(zhuǎn)換(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互轉(zhuǎn)換)
- python Yaml、Json、Dict之間的轉(zhuǎn)化
相關(guān)文章
Django實現(xiàn)auth模塊下的登錄注冊與注銷功能
這篇文章主要介紹了Django實現(xiàn)auth模塊下的登錄注冊與注銷功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10淺談Python數(shù)據(jù)類型判斷及列表腳本操作
下面小編就為大家?guī)硪黄獪\談Python數(shù)據(jù)類型判斷及列表腳本操作。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11Python與xlwings黃金組合處理Excel各種數(shù)據(jù)和自動化任務(wù)
這篇文章主要為大家介紹了Python與xlwings黃金組合處理Excel各種數(shù)據(jù)和自動化任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>2023-12-12《Python學(xué)習(xí)手冊》學(xué)習(xí)總結(jié)
本篇文章是讀者朋友在學(xué)習(xí)了《Python學(xué)習(xí)手冊》這本書以后,總結(jié)出的學(xué)習(xí)心得,值得大家參考學(xué)習(xí)。2018-01-01python爬取之json、pickle與shelve庫的深入講解
這篇文章主要給大家介紹了關(guān)于python爬取之json、pickle與shelve庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Python使用BeautifulSoup庫解析網(wǎng)頁
在Python的網(wǎng)絡(luò)爬蟲中,網(wǎng)頁解析是一項重要的技術(shù)。而在眾多的網(wǎng)頁解析庫中,BeautifulSoup庫憑借其簡單易用而廣受歡迎,在本篇文章中,我們將學(xué)習(xí)BeautifulSoup庫的基本用法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2023-08-08