python递归迭代,通过嵌套json对python中的特定键进行递归迭代

I'm trying to pull nested values from a json file. I want to print out each of the values for every "id" key. I think I'm close but can't figure out why the obj type changes from a dict to a list, and then why I'm unable to parse that list.

Here is a link to the json I'm working with: http://hastebin.com/ratevimixa.tex

and here is my current code:

#!/usr/bin/env python

#-*- coding: utf-8 -*-

import json

json_data = open('JubJubProductions.json', 'r+')

jdata = json.loads(json_data.read().decode("utf-8"))

def recursion(dict):

for key, value in dict.items():

if type(value) == type(dict):

if key != "paging":

for key, value in value.items():

if isinstance (value,list):

print key

# place where I need to enter list comprehension?

if type(value) == type(dict):

if key == "id":

print " id found " + value

if key != "id":

print key + " 1st level"

if key == "id":

print key

else:

if key == "id":

print "id found " + value

if __name__ == '__main__':

recursion(jdata)

-------------------------------------------------------------------------------------------update

This is now what I'm working with and it'll return a single id value, but not all of them:

#!/usr/bin/env python

#-*- coding: utf-8 -*-

import json

json_data = open('jubjubProductions', 'r+')

jdata = json.loads(json_data.read().decode("utf-8"))

def id_generator(d):

for k, v in d.items():

if k == "id":

yield v

elif isinstance(v, dict):

for id_val in id_generator(v):

yield id_val

if __name__ == '__main__':

for _ in id_generator(jdata):

print (_)

解决方案def id_generator(dict_var):

for k, v in dict_var.items():

if k == "id":

yield v

elif isinstance(v, dict):

for id_val in id_generator(v):

yield id_val

This will create an iterator which will yield every value on any level under key "id". Example usage (printing all of those values):

for _ in id_generator(some_json_dict):

print(_)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值