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)

  

python字典解析的更多相关文章

  1. Python基础(冒泡、生成器、迭代器、列表与字典解析)

    一.冒泡算法 冒泡算法,给定一组数据,从大到小排序或者从小到大排序,就像气泡一样 原理:  相邻的两个对象相比,大的放到后面,交换位置 交换位置通过a,b=b,a来实现 1.我们可以通过for循环来根 ...

  2. python字典构造函数dict(mapping)解析

    Python字典的构造函数有三个,dict().dict(**args).dict(mapping),当中第一个.第二个构造函数比較好理解也比較easy使用, 而dict(mapping)这个构造函数 ...

  3. Python列表解析和字典解析

    python笔记_列表解析 相比于for循环,列表解析的语法是由底层c语言实现的,它和使用for循环遍历pyobject对象相比,性能会有很大的提升. 无条件子句的列表解析式 In [2]: [2*i ...

  4. 第4.4节 Python解析与推导:列表解析、字典解析、集合解析

    一.    引言 经过前几个章节的介绍,终于把与列表解析的前置内容介绍完了,本节老猿将列表解析.字典解析.集合解析进行统一的介绍. 前面章节老猿好几次说到了要介绍列表解析,但老猿认为涉及知识层面比较多 ...

  5. 用 ElementTree 在 Python 中解析 XML

    用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...

  6. Python XML解析(转载)

    Python XML解析 什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). 你可以通过本站学习XML教程 XML 被设计用来传输和存储数据. XML是 ...

  7. Python XML解析之ElementTree

    参考网址: http://www.runoob.com/python/python-xml.html https://docs.python.org/2/library/xml.etree.eleme ...

  8. python大法好——Python XML解析

    Python XML解析 什么是XML? XML 被设计用来传输和存储数据. XML是一套定义语义标记的规则,这些标记将文档分成许多部件并对这些部件加以标识. 它也是元标记语言,即定义了用于定义其他与 ...

  9. python字典类型

    字典类型简介 字典(dict)是存储key/value数据的容器,也就是所谓的map.hash.关联数组.无论是什么称呼,都是键值对存储的方式. 在python中,dict类型使用大括号包围: D = ...

随机推荐

  1. P3165 [CQOI2014]排序机械臂

    题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到高度最低的物品的位置 P1P_1P1​ ,并把左起第一个物品至 P1P_1P1 ...

  2. The Bells are Ringing UVALive - 4060(枚举求解)

    输出整数N,使得  t1 <= N  统计有多少组t1,t2,t3,满足:1<t1<t2<t3<=1000000,t3-t1<=25,且t1,t2,t3的最小公倍数 ...

  3. Oracle 事务实例(非理论)

     begin    begin    savepoint  p1;    ---------============   在这里写删改差语句(SELECT 不行)每句以分号结尾:如 delete ta ...

  4. Zju1610 Count the Colors

    题面: 画一些颜色段在一行上,一些较早的颜色就会被后来的颜色覆盖了. 你的任务就是要数出你随后能看到的不同颜色的段的数目. Input: 每组测试数据第一行只有一个整数n, 1 <= n < ...

  5. 【刷题】BZOJ 3531 [Sdoi2014]旅行

    Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足 从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰 ...

  6. 【刷题】BZOJ 4816 [Sdoi2017]数字表格

    Description Doris刚刚学习了fibonacci数列.用f[i]表示数列的第i项,那么 f[0]=0 f[1]=1 f[n]=f[n-1]+f[n-2],n>=2 Doris用老师 ...

  7. 【JQuery】JQuery属性

    一.前言         接着上一章的内容,继续本章的学习. 二.内容 $().jquery 返回的字符串包含jquery的版本号 jQuery.fx.interval 改变以毫秒计的动画运行速率 j ...

  8. Python之旅:字典

      Python数据类型 #作用:存多个值,key:value 存取,取值速度快 #定义:key必须是不可变类型,value可以是任意类型 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个 ...

  9. RF - selenium - open browser

    *** Settings ***Library Selenium2Library *** Test Cases ***Open baidu with Chrome Open Browser http: ...

  10. Linux各种重要配置文件详解

    1:网卡文件/etc/sysconfig/network-scripts/ifcfg-eth0 [root@Gin scripts]# cat /etc/sysconfig/network-scrip ...