递归函数来创​​建分层的JSON对象? [英] Recursive function to create hierarchical JSON object?

查看:161
本文介绍了递归函数来创​​建分层的JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是没有足够好的计算机科学家推测这一点我自己:(

我有一个返回是这样的JSON响应的API:

  //调用/ API / GET / 200
{编号:200,名称:法国,childNode:[ID:400,ID:500]}
//调用/ API / GET / 400
{编号:400,名称:巴黎,childNode:[ID:882,ID:417]}
//调用/ API / GET / 500
{编号:500,名称:里昂,childNode:[ID:998,ID:104]}
// 等等
 

我想递归解析,并建立一个分层的JSON对象,它看起来是这样的:

  {编号:200,
  名称:法国,
  孩子:
     {编号:400,
       名称:巴黎,
       孩子:[...]
     },
     {编号:500,
       名称:里昂,
       孩子:[...]
     }
  ]
}
 

到目前为止,我有这个,这确实解析树的每一个节点,但不保存成一个JSON对象。我怎样才能扩大这个将其保存成JSON对象?

 等级= {}
高清get_child_nodes(NODE_ID):
    请求= urllib2.Request(ROOT_URL + NODE_ID)
    响应= json.loads(urllib2.urlopen(要求).read())
    对于childnode响应['childNode']:
        temp_obj = {}
        temp_obj ['身份证'] = childnode ['身份证']
        temp_obj ['名称'] = childnode ['名称']
        儿童= get_child_nodes(temp_obj ['身份证'])
     //如何保存temp_obj到层级?
get_child_nodes(ROOT_NODE)
 

这是不是功课,但也许我需要做一些功课来获得,在解决这类问题更好:(感谢您的帮助。

解决方案

 高清get_node(NODE_ID):
    请求= urllib2.Request(ROOT_URL + NODE_ID)
    响应= json.loads(urllib2.urlopen(要求).read())
    temp_obj = {}
    temp_obj ['身份证'] =回应['身份证']
    temp_obj ['名称'] =回应['名称']
    temp_obj ['孩子'] = [get_node(子['身份证'])为孩子在回应['childNode']
    返回temp_obj

等级= get_node(ROOT_NODE)
 

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 /api/get/200
{ id : 200, name : 'France', childNode: [ id: 400, id: 500] } 
// call to /api/get/400
{ id : 400, name : 'Paris', childNode: [ id: 882, id: 417] } 
// call to /api/get/500
{ id : 500, name : 'Lyon', childNode: [ id: 998, id: 104] } 
// etc

I would like to parse it recursively and build a hierarchical JSON object that looks something like this:

{ id: 200,
  name: 'France', 
  children: [
     { id: 400,
       name: 'Paris',
       children: [...]
     },
     { id: 500,
       name: 'Lyon', 
       children: [...]
     } 
  ],
} 

So far, I have this, which does parse every node of the tree, but doesn't save it into a JSON object. How can I expand this to save it into the JSON object?

hierarchy = {}
def get_child_nodes(node_id):   
    request = urllib2.Request(ROOT_URL + node_id)
    response = json.loads(urllib2.urlopen(request).read())
    for childnode in response['childNode']:
        temp_obj = {}
        temp_obj['id'] = childnode['id']
        temp_obj['name'] = childnode['name']
        children = get_child_nodes(temp_obj['id'])
     // How to save temp_obj into the hierarchy?
get_child_nodes(ROOT_NODE)

This isn't homework, but maybe I need to do some homework to get better at solving this kind of problem :( Thank you for any help.

解决方案

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)

这篇关于递归函数来创​​建分层的JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆