主体:
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": "u524du7aefu4e0bu4e00u4ee3u5f00u53d1u8bedu8a00TypeScript u4eceu57fau7840u5230axiosu5b9eu6218",
"pic": "http://szimg.mukewang.com/5cf721df09fc2be500000000.jpg",
"links": ""
},
{
"id": 1648,
"type": 6,
"type_id": 169,
"name": "Python3u5165u95e8u673au5668u5b66u4e60 u7ecfu5178u7b97u6cd5u4e0eu5e94u7528",
"pic": "http://szimg.mukewang.com/5d0ed2d9085bd6ed09000300.jpg",
"links": ""
}, {
"id": 1875,
"type": 6,
"type_id": 316,
"name": "u4eceu57fau7840u5230u5b9eu6218 u624bu628au624bu5e26u4f60u638cu63e1u65b0u7248Webpack4.0",
"pic": "http://szimg.mukewang.com/5d0ed2ca086a9e6f09000300.jpg",
"links": ""
}, {
"id": 1999,
"type": 6,
"type_id": 342,
"name": "u7eafu6b63u5546u4e1au7ea7u5e94u7528 Node.js Koa2u5f00u53d1u5faeu4fe1u5c0fu7a0bu5e8fu670du52a1u7aef",
"pic": "http://szimg.mukewang.com/5ceb5d370955f30f09000300.jpg",
"links": ""
}, {
"id": 2158,
"type": 99,
"type_id": 0,
"name": "Spring Cloudu5faeu670du52a1u5f00u53d1u5b9eu8df5",
"pic": "http://img2.mukewang.com/5d088c4009bbebc009000300.jpg",
"links": "https://www.imooc.com/read/37"
}, {
"id": 1709,
"type": 6,
"type_id": 354,
"name": "Node.jsu5f00u53d1u4effu77e5u4e4eu670du52a1u7aef u6df1u5165u7406u89e3RESTful 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": "u6210u529f",
"timestamp": 1561269343507
}
if __name__ == "__main__":
print(search_json.search_key(data,"id"))