import json
# coding: utf-8
from functools import singledispatch
from collections import abc
import inspect
import traceback
{"CERT_ID":"12312424","CERT_NAME":"测试","PROD_ID":"CREDIT","MP":"10086","TRANS_INFO":"1232434355","DATA":[{"attributes": {"CR_PS_MC_LM24":0.0,"CR_TR_TR_LM24":0.0,"CD_AL_IS_LM24":2.0,"CD_CC_AL_LM12":1.0,"CR_DC_OGO2_LM12":0.0,"CR_EX_EP_LM06":0.0,"CR_CC_CS_LM03":0.0}},{"blacklist": [{"orgLostContact":null,"bankLostContact":null,"seriousOverdueTime":"20170422","dunTelCallTime":"20170829","orgBlackList":[{"imsi":"ADFDAFEF243","org_code":"010005","value":null},{"imsi":"ADFDAFEF243","org_code":"434344","value":null},{"imsi":"ADFDAFEF243","org_code":"5335355","value":null}],"orgOverduePeriod":null,"bankOverduePeriod":null,"orgLitigation":null,"bankLitigation":null,"orgOneMonthOvedue":null,"matchType":"phone","matchValue":"ASRWVDSD1224","matchId":"WGDSGGTY223214"}]},{"loan": {"record":[{"matchType":"phone","matchValue":"10086","matchId":"WGDSGGTY223214","classification":[{"M3":{"other":{"orgNums":2,"loanAmount":null,"totalAmount":null,"repayAmount":null,"latestLoanTime":null},"bank":null}},{"M6":{"other":{"orgNums":1,"loanAmount":null,"totalAmount":null,"repayAmount":null,"latestLoanTime":null},"bank":null}},{"M9":{"other":{"orgNums":1,"loanAmount":null,"totalAmount":"(2000, 3000]","repayAmount":"(200, 500]","latestLoanTime":null},"bank":null}},{"M12":{"other":{"orgNums":1,"loanAmount":null,"totalAmount":"(500, 1000]","repayAmount":"(200, 500]","latestLoanTime":null},"bank":null}}],"latestRepaySuccessTime":null}]}},{"overdue": {"record":[{"matchType":"phone","matchValue":"10086","matchId":"FRGDGFSHKYJTGR13214","classification":[{"M12":{"bankCredit":null,"otherLoan":{"orgNums":2,"recordNums":4,"maxAmount":"(200, 500]","longestDays":null,"longestDaysTime":null},"otherCredit":null,"bankLoan":{"orgNums":3,"recordNums":5,"maxAmount":null,"longestDays":"3","longestDaysTime":"2017-08-30"}}},{"M24":{"bankCredit":null,"otherLoan":null,"otherCredit":null,"bankLoan":{"orgNums":1,"recordNums":2,"maxAmount":null,"longestDays":"60","longestDaysTime":"2017-04-22"}}}],"latestTime":"2017-08-31"}]}}]}
data = json.loads(jsonstr)
new_dict = {}
m_dict = {}
@singledispatch
def get_data_dict(obj):
return obj
@get_data_dict.register(dict)
def _(obj):
# print("dict", obj)
for k, v in obj.items():
if k in m_dict.keys():
m_dict.setdefault(f"{k}_1", v)
else:
m_dict.setdefault(k, v)
get_data_dict(v)
@get_data_dict.register(abc.MutableSequence)
def _(obj):
# print("seq", obj)
for item in obj:
get_data_dict(item)
class Get_Data:
# 获取类名
def get_current_function_name(self):
return inspect.stack()[1][3]
def __init__(self):
pass
def attributes(self, obj):
# print(self.attributes.__code__.co_varnames) #可以获取当前方法有多少参数。
print("当前方法", self.get_current_function_name())
{new_dict.setdefault(k, v) for k, v in obj.items()}
def blacklist(self, obj):
print("当前方法", self.get_current_function_name())
for dicts in obj:
for k, v in dicts.items():
if isinstance(v, list):
break
else:
new_dict.setdefault(k, v)
def M(self, m, obj):
print("当前方法", self.get_current_function_name())
for k, v in obj.items():
if isinstance(v, dict):
for k2, v2 in v.items():
m_key = f"{m}_{k2}"
new_dict.setdefault(m_key, v2)
else:
new_dict.setdefault(k, v)
def get_method(self):
return [i for i in dir(self) if not i.startswith(("__", "get"))]
g = Get_Data()
method_list = g.get_method()
for k, v in data.items():
if k == "DATA":
data_dict = data[k]
get_data_dict(data_dict)
else:
new_dict.setdefault(k, v)
for k, v in m_dict.items():
try:
if k in method_list or k.startswith("M"):
if k.startswith("M"):
eval("g." + "M")(k, v)
else:
eval("g." + k)(v)
except:
print(traceback.format_exc())
print(new_dict)