Python读取JSON
本文最后更新于:2022年4月24日 下午
参考博客:python 读写json文件 使用python读取json文件
Python多维/嵌套字典数据无限遍历
python json文件遍历所有key、value 及替换key对于的value
$\times$
json数据部分展示:
读取多个JSON数据,提取不同已知属性的值:
代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import json
import time
start=time.time()
with open('C:/Users/Administrator/Desktop/extract.json', encoding='utf-8') as f:
tdid=[]
androidId=[]
imei=[]
for line in f.readlines():
#print(line)
#line=f.readline()
d = json.loads(line)
tdid.append(d['tdid'])
androidId.append(d['id']['androidId'])
#imei.append(d['id']['sim'][1]['imei'])
timeUsed=time.time()-start#计时
print(tdid,androidId)读取单个JSON数据,提取该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
28import json
with open('C:/Users/Administrator/Desktop/XS/new.json', encoding='utf-8') as f:
jsonValue=[]
#for line in f.readlines():
#print(line)
line=f.readline()
d = json.loads(line)#转换为字典
def jsonAll(d):
if isinstance(d,dict) or isinstance(d,list):#判断是否为字典或列表
for key in d:
#print(key)
if isinstance(d,dict):
tempValue = d[key]
else:
tempValue = d[0]
if isinstance(tempValue,dict) or#判断是否为字典或列表 isinstance(tempValue,list):
#print(tempValue)
#if isinstance(tempValue,dict):
jsonAll(tempValue)#自我调用
#print("%s :%s" %(key,tempValue))
else:
jsonValue.append(tempValue)
#print(tempValue)
#jsonValue += tempValue
return jsonValue
jsonValue=jsonAll(d)
print(jsonValue)输出结果:
1
['317ee02415b4af4311787abf4382eb075', 1513069978654, 'ta', 2, None, '9ee0e03e4f552ac5', None, None, None, None, 'AC:C1:EE:33:D9:D8', 'fb088626', '869718024877240', '460008382291304', '89860084191445414192', '460', '00', '1', '869718024877240', '460008382291304', '89860084191445414192', '460', '00', '1', '69264580E09186B40701BB968CE87FFC', 'Soul', '2.8.6', 'cn.soulapp.android', 1507785806909, 0, 'Android+TalkingData+V4.0.12', 'Android+6.0.1', '23', 'zh', 'zh_CN', 8.0, 23.008712, 113.748479, 0.0, 1513069558940, 29.0, 0.0, 0.0, 0.0, 'network', 'cellular', 'LTE', None, '2147483647', '2147483647', 2147483647, 2147483647, 23, False, 'LTE', None, '2147483647', '2147483647', 2147483647, 2147483647, 23, False, 'LTE', None, '2147483647', '2147483647', 2147483647, 2147483647, 23, False, 'LTE', None, '2147483647', '2147483647', 2147483647, 2147483647, 23, False, 'LTE', None, '2147483647', '2147483647', 2147483647, 2147483647, 23, False, 'LTE', None, '2147483647', '2147483647', 2147483647, 2147483647, 23, False, 'LTE', None, '2147483647', '2147483647', 2147483647, 2147483647, 23, False, '223.104.1.147', 'Xiaomi', 'MI 5s', '1080*1920*480', 'Qualcomm Technologies, Inc MSM8996pro', 2, 1996800.0, 2779932, 112738896, 0, 0, 2, 1511918381515, None, None]
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!