-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex_04_services_json.py
More file actions
33 lines (23 loc) · 884 Bytes
/
ex_04_services_json.py
File metadata and controls
33 lines (23 loc) · 884 Bytes
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
import requests
import json
def process_json(fname):
f = open(fname, 'r')
sens = json.load(f) #Rückgabewert ist hier eine (assoziative) Liste
pretty = json.dumps(sens, indent=4, sort_keys=True) #Achtung: Rückgabewert ist ein String
print(pretty)
first = sens[0] #Hole den ersten Eintrag aus der Liste
print(first)
#Auslesen aller Namen
for s in sens:
print(s['key'])
#Auslesen aller Sensortypen
for s in sens:
entries = s['sensor']['array']
for entry in entries:
if 'type' in entry: #So überprüft man ob es einen Eintrag gibt (allgemein, ob ein key in einem Dictionary vorhanden ist
print(entry['type'])
f.close()
with open('%s.first' % fname, 'w') as outfile:
json.dump(first, outfile)
if __name__ == '__main__':
process_json('/home/albert/tmp/sensor.json')