广告位联系
返回顶部
分享到

python json递归打印所有json子节点信息

python 来源:互联网搜集 作者:秩名 发布时间:2020-02-27 15:46:32 人浏览
摘要

直接上代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def json_txt( self , dic_json): #self.debug_print(json_txt) if isinstance (dic_json, dict ): # 判断是否是字典类型isinstance 返回True false for key in dic_json: #dic_json = json.loads(s)

直接上代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def json_txt(self, dic_json):
 #self.debug_print("json_txt")
 if isinstance(dic_json, dict): # 判断是否是字典类型isinstance 返回True false
  for key in dic_json:
   #dic_json = json.loads(s)
   s = dic_json[key]
   #self.debug_print(str(len(s)) + " type:" + str(type(s)))
   t=str(type(s))
   if t.startswith("<class 'list'>"):
    for i in range(0, len(s)):
     self.debug_print("%s %d:" % (key, i))
     self.json_txt(s[i])
   else:
    self.debug_print("%s: %s" % (key, s))
 else:
  self.debug_print("else")

补充拓展:python求json某层节点的和实例

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
 
from collections import Mapping
 
import json
 
def dict_generator(indict, pre=None):
pre = pre[:] if pre else []
if isinstance(indict, dict):
for key, value in indict.items():
if isinstance(value, dict):
if len(value) == 0:
yield pre+[key, ‘{}']
else:
for d in dict_generator(value, pre + [key]):
yield d
elif isinstance(value, list):
if len(value) == 0:
yield pre+[key, ‘[]']
else:
for v in value:
for d in dict_generator(v, pre + [key]):
yield d
elif isinstance(value, tuple):
if len(value) == 0:
yield pre+[key, ‘()']
else:
for v in value:
for d in dict_generator(v, pre + [key]):
yield d
else:
yield pre + [key, value]
else:
yield indict
 
def recursive_findall(obj, key, paths=None):
ret = []
if not paths:
paths = []
if isinstance(obj, Mapping):
for k, v in obj.iteritems():
found_items = recursive_findall(v, key, paths=(paths + [(‘k', k)]))
ret += found_items
elif isinstance(obj, (list, tuple)):
for i, v in enumerate(obj):
found_items = recursive_findall(v, key, paths=(paths + [(‘i', i)]))
ret += found_items
else:
if key(obj):
ret.append((paths, obj))
return ret
 
ret_dict = {
“data”:[
{
“email”:"",
“repoCommits”:[
{
“branchCommitLine”:[
{
“submitLine”:1
},
{
“submitLine”: 1
}]},
{
“branchCommitLine”: [
{
“submitLine”: 1
},
{
“submitLine”: 1
}]}]
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
       },
{
 "email": "",
 "repoCommits": [
  {
   "branchCommitLine": [
    {
     "submitLine": 1
    },
    {
     "submitLine": 1
    }]},
  {
   "branchCommitLine": [
    {
     "submitLine": 1
    },
    {
     "submitLine": 1
    }]}]
 
}
 
 
 
   ]
  }

if name == ‘main':

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
all_socre = 0
for da in ret_dict["data"]:
 if da.has_key("repoCommits"):
  for repo in da["repoCommits"]:
   if repo.has_key("branchCommitLine"):
    for branch in repo["branchCommitLine"]:
     if branch.has_key("submitLine"):
      all_socre += int(branch["submitLine"])
     else:
      continue
   else:
    continue
 else:
  continue
ret = dict_generator(ret_dict)
print(ret)
for i in ret:
 print i[-1]


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/r77683962/article/details/93652794
相关文章
  • Python Django教程之实现新闻应用程序

    Python Django教程之实现新闻应用程序
    Django是一个用Python编写的高级框架,它允许我们创建服务器端Web应用程序。在本文中,我们将了解如何使用Django创建新闻应用程序。 我们将
  • 书写Python代码的一种更优雅方式(推荐!)

    书写Python代码的一种更优雅方式(推荐!)
    一些比较熟悉pandas的读者朋友应该经常会使用query()、eval()、pipe()、assign()等pandas的常用方法,书写可读性很高的「链式」数据分析处理代码
  • Python灰度变换中伽马变换分析实现

    Python灰度变换中伽马变换分析实现
    1. 介绍 伽马变换主要目的是对比度拉伸,将图像灰度较低的部分进行修正 伽马变换针对的是对单个像素点的变换,也就是点对点的映射 形
  • 使用OpenCV实现迷宫解密的全过程

    使用OpenCV实现迷宫解密的全过程
    一、你能自己走出迷宫吗? 如下图所示,可以看到是一张较为复杂的迷宫图,相信也有人尝试过自己一点一点的找出口,但我们肉眼来解谜
  • Python中的数据精度问题的介绍

    Python中的数据精度问题的介绍
    一、python运算时精度问题 1.运行时精度问题 在Python中(其他语言中也存在这个问题,这是计算机采用二进制导致的),有时候由于二进制和
  • Python随机值生成的常用方法

    Python随机值生成的常用方法
    一、随机整数 1.包含上下限:[a, b] 1 2 3 4 import random #1、随机整数:包含上下限:[a, b] for i in range(10): print(random.randint(0,5),end= | ) 查看运行结
  • Python字典高级用法深入分析讲解
    一、 collections 中 defaultdict 的使用 1.字典的键映射多个值 将下面的列表转成字典 l = [(a,2),(b,3),(a,1),(b,4),(a,3),(a,1),(b,3)] 一个字典就是一个键对
  • Python浅析多态与鸭子类型使用实例
    什么多态:同一事物有多种形态 为何要有多态=》多态会带来什么样的特性,多态性 多态性指的是可以在不考虑对象具体类型的情况下而直
  • Python字典高级用法深入分析介绍
    一、 collections 中 defaultdict 的使用 1.字典的键映射多个值 将下面的列表转成字典 l = [(a,2),(b,3),(a,1),(b,4),(a,3),(a,1),(b,3)] 一个字典就是一个键对
  • Python淘宝或京东等秒杀抢购脚本实现(秒杀脚本

    Python淘宝或京东等秒杀抢购脚本实现(秒杀脚本
    我们的目标是秒杀淘宝或京东等的订单,这里面有几个关键点,首先需要登录淘宝或京东,其次你需要准备好订单,最后要在指定时间快速
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计