Python json 사용자 정의 인코딩 및 디코딩 기능

코드를 직접 업로드하세요.

사용자 정의 인코딩 및 디코딩 기능, 여기서는 시간 유형 데이터 인코딩 및 디코딩을 예로 들어 보겠습니다.

def json_decode_datetime(obj):
    if isinstance(obj, datetime):
        return {
    
    
            "json_decode_datetime":  datetime.timestamp(obj)
        }
    return obj


def json_encode_datetime(dic):
    if dic.get("json_decode_datetime", None):
        return datetime.fromtimestamp(dic['json_decode_datetime'])
    return dic

json은 인코딩 및 디코딩 기능을 호출합니다.

dict_data = json.loads(cache, object_hook=json_encode_datetime)
res_str = json.dumps(frame_full_info.dict(), default=json_decode_datetime)

Supongo que te gusta

Origin blog.csdn.net/Defiler_Lee/article/details/118299477
Recomendado
Clasificación