zoukankan      html  css  js  c++  java
  • 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": "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"))

  • 相关阅读:
    spring的APO
    设计系统测试计划需要参考的项目文档有
    系统测试的策略
    软件测试包括
    简述你在以前的工作中做过哪些事情,比较熟悉什么
    在C/C++中static有什么用途?(请至少说明两种)
    引用与指针有什么区别?
    说说你对集成测试中自顶向下集成和自底向上集成两个策略的理解,要谈出它们各自的优缺点和主要适应于哪种类型测试;
    系统测试的策略
    通过画因果图来写测试用例的步骤为___、___、___、___及把因果图转换为状态图共五个步骤。 利用因果图生成测试用例的基本步骤是:
  • 原文地址:https://www.cnblogs.com/cyanrose/p/11596598.html
Copyright © 2011-2022 走看看