from django.test import TestCase
import requests
global_dict = dict()
def insert_data(data):
"""递归 把所有部门对应的id_hierarchy取出放入一个大字典中"""
global_dict[data["id"]] = data["id_hierarchy"]
sub_departments = data["sub_departments"]
if sub_departments:
for sub_data in sub_departments:
insert_data(sub_data)
def get_data(ids):
url = 'https://access.test.starmerx.com/api/departments/'
headers = {
'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"}
response = requests.get(url=url, headers=headers)
page_text = response.json()
data = page_text['data']
insert_data(data)
num = 0
# 把最下层级数找出来
d = None
for id in ids:
d = {}
if num <= global_dict.get(id).count(&#
多层字典嵌套 取相应的数据
最新推荐文章于 2024-02-18 14:53:16 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)