json递归查询

主体:
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"))

posted @ 2019-09-27 11:05  青色蔷薇  阅读(597)  评论(0编辑  收藏  举报