Python中Json和其他類型相互轉(zhuǎn)換的實(shí)現(xiàn)示例
項(xiàng)目中經(jīng)常會(huì)用到json格式轉(zhuǎn)為object對(duì)象、dict字典格式等。在此做個(gè)記錄,方便后續(xù)用到該方法可以快速?gòu)?fù)制粘貼。
需要用到json包。
loads():將json數(shù)據(jù)轉(zhuǎn)換成為dict字典數(shù)據(jù)load(): 讀取json文件數(shù)據(jù),轉(zhuǎn)成dict數(shù)據(jù)dumps():將dict數(shù)據(jù)轉(zhuǎn)化成json數(shù)據(jù)dump(): 將dict數(shù)據(jù)轉(zhuǎn)換成為json數(shù)據(jù)之后再寫入json文件
"""
@Description: json和其他類型之間互轉(zhuǎn)
"""
import json
import os
class JsonToOtherType:
"""
@ClassName: JsonToOtherType
@Description: Json數(shù)據(jù)和其他數(shù)據(jù)之間互轉(zhuǎn)
loads():將json數(shù)據(jù)轉(zhuǎn)換成為dict字典數(shù)據(jù)
load():讀取json文件數(shù)據(jù),轉(zhuǎn)成dict數(shù)據(jù)
dumps():將dict數(shù)據(jù)轉(zhuǎn)化成json數(shù)據(jù)
dump():將dict數(shù)據(jù)轉(zhuǎn)換成為json數(shù)據(jù)之后再寫入json文件
"""
def __init__(self):
"""
@Description:
"""
pass
# 設(shè)置為靜態(tài)方法
@staticmethod
def dict_to_json(dict_data:dict):
"""
@Description: dict字典轉(zhuǎn)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: 對(duì)象轉(zhuǎn)json數(shù)據(jù)
obj.__dict__:將對(duì)象轉(zhuǎn)換成為dict字典類型
json.dumps():再將字典類型轉(zhuǎn)化為json數(shù)據(jù)
:param obj_data 對(duì)象數(shù)據(jù)
"""
try:
dict_data = obj_data.__dict__
#轉(zhuǎn)換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ù)轉(zhuǎn)換成為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ù)轉(zhuǎn)換成為obj對(duì)象
1.先將json數(shù)據(jù)轉(zhuǎn)換成為dict字典數(shù)據(jù)
2.再將字典數(shù)據(jù)轉(zhuǎn)換成為obj對(duì)象
:param json_data json數(shù)據(jù)
:param obj 需要轉(zhuǎn)換的obj對(duì)象
:return obj 返回的對(duì)象
"""
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ù)轉(zhuǎn)換成為json數(shù)據(jù)之后再寫入json文件
dump():將dict數(shù)據(jù)轉(zhuǎn)換成為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:
# 會(huì)在對(duì)應(yīng)路徑下生成一個(gè)filename名字的文件,文件內(nèi)容是dict字典數(shù)據(jù)轉(zhuǎn)換成的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ù),將文件內(nèi)容轉(zhuǎn)換成為dict字典數(shù)據(jù)
load():讀取json文件數(shù)據(jù),轉(zhuǎn)成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
到此這篇關(guān)于Python中Json和其他類型相互轉(zhuǎn)換的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python Json和其他類型相互轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python中將字典轉(zhuǎn)換成其json字符串
- python將字符串轉(zhuǎn)換成json的方法小結(jié)
- Python對(duì)象轉(zhuǎn)換為json的方法步驟
- python 對(duì)象和json互相轉(zhuǎn)換方法
- python實(shí)現(xiàn)class對(duì)象轉(zhuǎn)換成json/字典的方法
- 基于python實(shí)現(xiàn)把json數(shù)據(jù)轉(zhuǎn)換成Excel表格
- Python中xml和json格式相互轉(zhuǎn)換操作示例
- Python實(shí)現(xiàn)把json格式轉(zhuǎn)換成文本或sql文件
- 使用python把json文件轉(zhuǎn)換為csv文件
- 使用python將mysql數(shù)據(jù)庫(kù)的數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù)的方法
相關(guān)文章
Python采集C站熱榜數(shù)據(jù)實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Python采集C站熱榜數(shù)據(jù)實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Python multiprocessing 進(jìn)程間通信方式實(shí)現(xiàn)
本文主要介紹了Python multiprocessing 進(jìn)程間通信方式實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
如何使用 Flask 做一個(gè)評(píng)論系統(tǒng)
這篇文章主要介紹了如何使用 Flask 做一個(gè)評(píng)論系統(tǒng),幫助大家更好的理解和使用flask框架進(jìn)行python web開發(fā),感興趣的朋友可以了解下2020-11-11
如何利用Python分析出微信朋友男女統(tǒng)計(jì)圖
這篇文章主要給大家介紹了關(guān)于如何利用Python分析出微信朋友男女統(tǒng)計(jì)圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2019-01-01
Python?xlwt工具使用詳解,生成excel欄位寬度可自適應(yīng)內(nèi)容長(zhǎng)度
這篇文章主要介紹了Python?xlwt工具使用詳解,生成excel欄位寬度可自適應(yīng)內(nèi)容長(zhǎng)度,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

