Python中Json和其他類型相互轉換的實現(xiàn)示例
更新時間:2025年08月21日 11:22:55 作者:一個小坑貨
本文介紹了在Python中使用json模塊實現(xiàn)json數(shù)據(jù)與dict、object之間的高效轉換,包括loads(), load(), dumps(), dump()方法,以及文件讀寫操作,感興趣的可以了解一下
項目中經(jīng)常會用到json格式轉為object對象、dict字典格式等。在此做個記錄,方便后續(xù)用到該方法可以快速復制粘貼。
需要用到json包。
loads():將json數(shù)據(jù)轉換成為dict字典數(shù)據(jù)load(): 讀取json文件數(shù)據(jù),轉成dict數(shù)據(jù)dumps():將dict數(shù)據(jù)轉化成json數(shù)據(jù)dump(): 將dict數(shù)據(jù)轉換成為json數(shù)據(jù)之后再寫入json文件
"""
@Description: json和其他類型之間互轉
"""
import json
import os
class JsonToOtherType:
"""
@ClassName: JsonToOtherType
@Description: Json數(shù)據(jù)和其他數(shù)據(jù)之間互轉
loads():將json數(shù)據(jù)轉換成為dict字典數(shù)據(jù)
load():讀取json文件數(shù)據(jù),轉成dict數(shù)據(jù)
dumps():將dict數(shù)據(jù)轉化成json數(shù)據(jù)
dump():將dict數(shù)據(jù)轉換成為json數(shù)據(jù)之后再寫入json文件
"""
def __init__(self):
"""
@Description:
"""
pass
# 設置為靜態(tài)方法
@staticmethod
def dict_to_json(dict_data:dict):
"""
@Description: dict字典轉json數(shù)據(jù)
:param dict_data 字典數(shù)據(jù)
:return json_data 返回json類型數(shù)據(jù)
"""
try:
json_data = json.dumps(dict_data)
except Exception as e:
return e
return json_data
@staticmethod
def obj_to_json(obj_data:object):
"""
@Description: 對象轉json數(shù)據(jù)
obj.__dict__:將對象轉換成為dict字典類型
json.dumps():再將字典類型轉化為json數(shù)據(jù)
:param obj_data 對象數(shù)據(jù)
"""
try:
dict_data = obj_data.__dict__
#轉換dict字典類型
json_data = json.dumps(obj=dict_data)
except Exception as e:
return e
return json_data
@staticmethod
def json_to_dict(json_data:str):
"""
@Description: json數(shù)據(jù)轉換成為dict字典數(shù)據(jù)
:param json_data json數(shù)據(jù)
:return dict_data 返回dict字典數(shù)據(jù)
"""
try:
dict_data = json.loads(s=json_data)
except Exception as e:
return e
return dict_data
@staticmethod
def json_to_obj(json_data:str,obj:object):
"""
@Description: json數(shù)據(jù)轉換成為obj對象
1.先將json數(shù)據(jù)轉換成為dict字典數(shù)據(jù)
2.再將字典數(shù)據(jù)轉換成為obj對象
:param json_data json數(shù)據(jù)
:param obj 需要轉換的obj對象
:return obj 返回的對象
"""
try:
dict_data = json.loads(s=json_data)
obj.__dict__ = dict_data
except Exception as e:
return e
return obj
@staticmethod
def dict_to_json_write_file(dict_data:dict,filepath:str,filename:str):
"""
@Description: 將dict數(shù)據(jù)轉換成為json數(shù)據(jù)之后再寫入json文件
dump():將dict數(shù)據(jù)轉換成為json數(shù)據(jù)之后再寫入json文件
:param dict_data 字典數(shù)據(jù)
:param filepath 文件路徑
:param filename 文件名字
"""
try:
with open (os.path.join(filepath,filename),'w',encoding='utf-8') as f:
# 會在對應路徑下生成一個filename名字的文件,文件內容是dict字典數(shù)據(jù)轉換成的json數(shù)據(jù)
json.dump(dict_data,f)
except Exception as e:
return e
@staticmethod
def json_file_to_dict(filepath:str,filename:str):
"""
@Description: 讀取json文件中的數(shù)據(jù),將文件內容轉換成為dict字典數(shù)據(jù)
load():讀取json文件數(shù)據(jù),轉成dict數(shù)據(jù)
:param filepath 文件路徑
:param filename 文件名字
:return dict_data 返回的字典數(shù)據(jù)
"""
try:
with open(os.path.join(filepath,filename),'r',encoding='utf-8') as f:
dict_data = json.load(fp=f)
except Exception as e:
return e
return dict_data
到此這篇關于Python中Json和其他類型相互轉換的實現(xiàn)示例的文章就介紹到這了,更多相關Python Json和其他類型相互轉換內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
分享PyCharm最新激活碼(真永久激活方法)不用每月找安裝參數(shù)或最新激活碼了
這篇文章主要介紹了分享PyCharm最新激活碼(真永久激活方法)不用每月找安裝參數(shù)或最新激活碼了一勞永逸,需要的朋友可以參考下2020-12-12
Python循環(huán)語句For?Range用法示例詳解
這篇文章主要為大家介紹了Python循環(huán)語句For?Range用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
python3操作redis實現(xiàn)List列表實例
本文主要介紹了python3操作redis實現(xiàn)List列表實例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Pytorch之nn.Upsample()和nn.ConvTranspose2d()用法詳解
nn.Upsample和nn.ConvTranspose2d是PyTorch中用于上采樣的兩種主要方法,nn.Upsample通過不同的插值方法(如nearest、bilinear)執(zhí)行上采樣,沒有可學習的參數(shù),適合快速簡單的尺寸增加,而nn.ConvTranspose2d通過可學習的轉置卷積核進行上采樣2024-10-10

