最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中Json和其他類型相互轉(zhuǎn)換的實(shí)現(xiàn)示例

 更新時(shí)間:2025年08月21日 11:22:55   作者:一個(gè)小坑貨  
本文介紹了在Python中使用json模塊實(shí)現(xiàn)json數(shù)據(jù)與dict、object之間的高效轉(zhuǎn)換,包括loads(), load(), dumps(), dump()方法,以及文件讀寫操作,感興趣的可以了解一下

項(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)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Python采集C站熱榜數(shù)據(jù)實(shí)戰(zhàn)示例

    Python采集C站熱榜數(shù)據(jù)實(shí)戰(zhàn)示例

    這篇文章主要為大家介紹了Python采集C站熱榜數(shù)據(jù)實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • python有證書的加密解密實(shí)現(xiàn)方法

    python有證書的加密解密實(shí)現(xiàn)方法

    這篇文章主要介紹了python有證書的加密解密實(shí)現(xiàn)方法,采用了M2Crypto組件進(jìn)行相關(guān)的加密解密操作,包含了詳細(xì)的完整實(shí)現(xiàn)過程,需要的朋友可以參考下
    2014-11-11
  • Python multiprocessing 進(jìn)程間通信方式實(shí)現(xiàn)

    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
  • python爬取新聞門戶網(wǎng)站的示例

    python爬取新聞門戶網(wǎng)站的示例

    短期目前旨在爬取所有新聞門戶網(wǎng)站的新聞,每個(gè)門戶網(wǎng)站爬蟲開箱即用,并自動(dòng)保存到同目錄下的 csv/excel 文件中,禁止將所得數(shù)據(jù)商用。
    2021-04-04
  • Python爬蟲代理IP池實(shí)現(xiàn)方法

    Python爬蟲代理IP池實(shí)現(xiàn)方法

    在公司做分布式深網(wǎng)爬蟲,搭建了一套穩(wěn)定的代理池服務(wù),為上千個(gè)爬蟲提供有效的代理,保證各個(gè)爬蟲拿到的都是對(duì)應(yīng)網(wǎng)站有效的代理IP,從而保證爬蟲快速穩(wěn)定的運(yùn)行,所以就想利用一些免費(fèi)的資源搞一個(gè)簡(jiǎn)單的代理池服務(wù)。
    2017-01-01
  • 如何使用 Flask 做一個(gè)評(píng)論系統(tǒng)

    如何使用 Flask 做一個(gè)評(píng)論系統(tǒng)

    這篇文章主要介紹了如何使用 Flask 做一個(gè)評(píng)論系統(tǒng),幫助大家更好的理解和使用flask框架進(jìn)行python web開發(fā),感興趣的朋友可以了解下
    2020-11-11
  • python實(shí)現(xiàn)單機(jī)五子棋

    python實(shí)現(xiàn)單機(jī)五子棋

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)單機(jī)五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 如何利用Python分析出微信朋友男女統(tǒng)計(jì)圖

    如何利用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)度

    這篇文章主要介紹了Python?xlwt工具使用詳解,生成excel欄位寬度可自適應(yīng)內(nèi)容長(zhǎng)度,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 在Django框架中偽造捕捉到的URLconf值的方法

    在Django框架中偽造捕捉到的URLconf值的方法

    這篇文章主要介紹了在Django框架中偽造捕捉到的URLconf值的方法,Django是Python各色人氣框架中最為著名的一個(gè),需要的朋友可以參考下
    2015-07-07

最新評(píng)論

泽库县| 两当县| 富锦市| 盈江县| 马公市| 西盟| 巴东县| 高清| 延寿县| 柳江县| 从江县| 中西区| 菏泽市| 鹰潭市| 安义县| 吉安市| 新巴尔虎左旗| 邳州市| 疏勒县| 凤凰县| 凤凰县| 嘉义县| 黄梅县| 鱼台县| 蛟河市| 贵德县| 和硕县| 满洲里市| 长武县| 阿拉善左旗| 商都县| 安阳县| 辽源市| 郎溪县| 离岛区| 普格县| 兴仁县| 九龙县| 兖州市| 长阳| 台北县|