current position:Home>JSON and other types convert to each other in Python

JSON and other types convert to each other in Python

2022-05-15 05:35:49A small pit of goods

json And other types of conversions

It's often used in projects json Format to object object 、dict Dictionary format, etc . Make a record here , Convenient for subsequent use, this method can quickly copy and paste .
Need to use json package .

  • loads(): take json Data conversion into dict Dictionary data
  • load(): Read json File data , Turn into dict data
  • dumps(): take dict Data conversion to json data
  • dump(): take dict Data conversion into json Write the data later json file
""" @Description: json And other types  """
import json
import os

class JsonToOtherType:
    """ @ClassName: JsonToOtherType @Description: Json Data and other data are transferred to each other  loads(): take json Data conversion into dict Dictionary data  load(): Read json File data , Turn into dict data  dumps(): take dict Data conversion to json data  dump(): take dict Data conversion into json Write the data later json file  """
    def __init__(self):
        """ @Description: """
        pass

    #  Set to static method 
    @staticmethod
    def dict_to_json(dict_data:dict):
        """ @Description: dict Dictionary transfer json data  :param dict_data  Dictionary data  :return json_data  return json Type data  """
        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:  Object turn json data  obj.__dict__: Convert objects to dict Dictionary type  json.dumps(): Then convert the dictionary type to json data  :param obj_data  Object data  """
        try:
            dict_data = obj_data.__dict__
            # transformation dict Dictionary type 
            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 Data conversion into dict Dictionary data  :param json_data json data  :return dict_data  return dict Dictionary data  """
        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 Data conversion into obj object  1. First the json Data conversion into dict Dictionary data  2. Then convert the dictionary data into obj object  :param json_data json data  :param obj  transformable obj object  :return obj  Returned object  """
        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:  take dict Data conversion into json Write the data later json file  dump(): take dict Data conversion into json Write the data later json file  :param dict_data  Dictionary data  :param filepath  File path  :param filename  File name  """
        try:
            with open (os.path.join(filepath,filename),'w',encoding='utf-8') as f:
                #  A... Will be generated under the corresponding path filename Name file , The content of the document is dict Dictionary data is converted into json data 
                json.dump(dict_data,f)
        except Exception as e:
            return e

    @staticmethod
    def json_file_to_dict(filepath:str,filename:str):
        """ @Description:  Read json The data in the file , Convert the contents of the file into dict Dictionary data  load(): Read json File data , Turn into dict data  :param filepath  File path  :param filename  File name  :return dict_data  Returned dictionary data  """
        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

If there is a mistake , Comments or private letters are welcome to point out , thank you !

copyright notice
author[A small pit of goods],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/135/202205142240049351.html

Random recommended