python生成递归json_创建分层JSON对象的递归函数?

我只是不是一个足够好的计算机科学家来自己解决这个问题

我有一个API,它返回如下所示的JSON响应:// 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

我想递归地解析它,并构建一个类似于以下内容的分层JSON对象:{ id: 200,

name: 'France',

children: [

{ id: 400,

name: 'Paris',

children: [...]

},

{ id: 500,

name: 'Lyon',

children: [...]

}

],

}

到目前为止,我已经有了这个,它确实解析了树的每个节点,但没有将其保存到JSON对象中。如何将其展开以保存到JSON对象中?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)

这不是家庭作业,但也许我需要做一些家庭作业来更好地解决这类问题:(谢谢你的帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值