主体:
class json_search():
'''递归查询依赖key'''
def search_key(self,data,key):
self.data = data
self.key_value = []
if self.data_json(data) != False:
self.search(self.data,key)
return self.key_value
else:
return False def data_json(self,data):
''' 入参判断'''
'''json是str子类'''
if isinstance(data,str):
try:
self.data=json.loads(data,encoding='utf-8')
except ValueError :
print("value error input")
return False
elif isinstance(data,dict):
return self.data
else:
return False
def search(self,data,key):
for i in data:
if i == key:
self.key_value.append(data[i])
elif isinstance(data[i],dict):
self.search(data[i],key)
elif isinstance(data[i],list):
for j in data[i]:
if isinstance(j,dict):
self.search(j,key) return self.key_value
示例:
search_json=json_search()

{
"status": 0,
"data": {
"hello": [2, 3, 4],
"banner": [{
"id": 2262,
"type": 6,
"type_id": 330,
"name": "\u524d\u7aef\u4e0b\u4e00\u4ee3\u5f00\u53d1\u8bed\u8a00TypeScript \u4ece\u57fa\u7840\u5230axios\u5b9e\u6218",
"pic": "http://szimg.mukewang.com/5cf721df09fc2be500000000.jpg",
"links": ""
},
{
"id": 1648,
"type": 6,
"type_id": 169,
"name": "Python3\u5165\u95e8\u673a\u5668\u5b66\u4e60 \u7ecf\u5178\u7b97\u6cd5\u4e0e\u5e94\u7528",
"pic": "http://szimg.mukewang.com/5d0ed2d9085bd6ed09000300.jpg",
"links": ""
}, {
"id": 1875,
"type": 6,
"type_id": 316,
"name": "\u4ece\u57fa\u7840\u5230\u5b9e\u6218 \u624b\u628a\u624b\u5e26\u4f60\u638c\u63e1\u65b0\u7248Webpack4.0",
"pic": "http://szimg.mukewang.com/5d0ed2ca086a9e6f09000300.jpg",
"links": ""
}, {
"id": 1999,
"type": 6,
"type_id": 342,
"name": "\u7eaf\u6b63\u5546\u4e1a\u7ea7\u5e94\u7528 Node.js Koa2\u5f00\u53d1\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u670d\u52a1\u7aef",
"pic": "http://szimg.mukewang.com/5ceb5d370955f30f09000300.jpg",
"links": ""
}, {
"id": 2158,
"type": 99,
"type_id": 0,
"name": "Spring Cloud\u5fae\u670d\u52a1\u5f00\u53d1\u5b9e\u8df5",
"pic": "http://img2.mukewang.com/5d088c4009bbebc009000300.jpg",
"links": "https://www.imooc.com/read/37"
}, {
"id": 1709,
"type": 6,
"type_id": 354,
"name": "Node.js\u5f00\u53d1\u4eff\u77e5\u4e4e\u670d\u52a1\u7aef \u6df1\u5165\u7406\u89e3RESTful API",
"pic": "http://szimg.mukewang.com/5d0ed27508f7d96909000300.jpg",
"links": ""
}
],
"pic": [{
"pic": "http://www.imooc.com/static/img/andriod/pic/actual_day@3x.png",
"pic_night": "http://www.imooc.com/static/img/andriod/pic/actual_night@3x.png",
"type": 2
}, {
"pic": "http://www.imooc.com/static/img/andriod/pic/path_day@3x.png",
"pic_night": "http://www.imooc.com/static/img/andriod/pic/path_night@3x.png",
"type": 6
}, {
"pic": "http://www.imooc.com/static/img/andriod/pic/question_day@3x.png",
"pic_night": "http://www.imooc.com/static/img/andriod/pic/question_night@3x.png",
"type": 3
}, {
"pic": "http://www.imooc.com/static/img/andriod/pic/note_day@3x.png",
"pic_night": "http://www.imooc.com/static/img/andriod/pic/note_night@3x.png",
"type": 4
}, {
"pic": "http://www.imooc.com/static/img/andriod/pic/discover_day@3x.png",
"pic_night": "http://www.imooc.com/static/img/andriod/pic/discover_night@3x.png",
"type": 5
}]
},
"errorCode": 1001,
"errorDesc": "\u6210\u529f",
"timestamp": 1561269343507
}

if __name__ == "__main__":

  print(search_json.search_key(data,"id"))

最新文章

  1. 如何使用的Ue4自带的SQLiteSupport
  2. 无线客户端框架设计(5.1):将JSON映射为实体对象(iOS篇)
  3. ACM题目————STL练习之 懒省事的小明(优先队列)
  4. 第一个Linux驱动-流水灯【转】
  5. Shell教程6-Shell注释
  6. 隐式Intent实例
  7. redhat6.4 配置centos6 yum替换
  8. Vijos1865 NOI2014 魔法森林 LCT维护生成树
  9. 【java】基于Tomcat的WebSocket转帖 + 自己理解
  10. 如何右键新建HTML
  11. HDU 3030 - Increasing Speed Limits
  12. iOSAPP添加启动页
  13. 专治不会看源码的毛病--spring源码解析AOP篇
  14. 数据库中字段类型对应的C#中的数据类型(转载)
  15. Storm 0.9安装指南
  16. unigui web app之菜单
  17. dubbo 源码编译记录
  18. CAN总线布线规范
  19. Mongo 后台加索引踩坑
  20. 常用 Math 属性及方法

热门文章

  1. Sass、LESS 和 Stylus各有千秋
  2. gmail 批量删除邮件
  3. 3种不走寻常路的黑客攻击泄露&如何保护自己?
  4. win7怎么使用远程桌面连接(win10类似,同样适用)
  5. 为什么 JavaScript 中 0.1+0.2 不等于 0.3 ?
  6. 【CF908D】New Year and Arbitrary Arrangement
  7. node http 模块 常用知识点记录
  8. 学习强国docker文件用法
  9. InnoSetup跨脚本传参数
  10. JVM GC监控