Recursive function to create hierarchical JSON object?

后端 未结 5 1975
庸人自扰
庸人自扰 2021-02-08 19:08

I\'m just not a good enough computer scientist to figure this out by myself :(

I have an API that returns JSON responses that look like this:

// call to         


        
5条回答
  •  长情又很酷
    2021-02-08 19:32

    def get_node(node_id):   
        request = urllib2.Request(ROOT_URL + node_id)
        response = json.loads(urllib2.urlopen(request).read())
        temp_obj = {}
        temp_obj['id'] = response['id']
        temp_obj['name'] = response['name']
        temp_obj['children'] = [get_node(child['id']) for child in response['childNode']]
        return temp_obj
    
    hierarchy = get_node(ROOT_NODE)
    

提交回复
热议问题