Hilfe bei JSON und Dictionary
Verfasst: Samstag 8. Februar 2020, 22:13
Hallo Leute, ich melde mich mal wieder seit langer Zeit hier im Forum mit einem Problem.
Ich habe eine JSON Datei wo ich mehrere Dict und Lists habe. Das einlesen und schreiben ist kein Problem. Leider komme ich nicht an die einzelnen Werte der JSON Datei. Ich habe es versucht mit parsen, rekursive und aktuell mit Ebenen. Ich bekomme im Moment ein Dictionary. Dieses bekomme ich auch nicht getrennt. Ziel des Ganzen wäre es z.B. einen String (conveyer__ip) bzw (conveyer__shuttle1__net_port) zu haben und das dazugehörige Value (12345) bzw. (13000). Ich hoffe ihr könnte mir ein wenig auf die Sprünge helfen.
LG
Ich habe eine JSON Datei wo ich mehrere Dict und Lists habe. Das einlesen und schreiben ist kein Problem. Leider komme ich nicht an die einzelnen Werte der JSON Datei. Ich habe es versucht mit parsen, rekursive und aktuell mit Ebenen. Ich bekomme im Moment ein Dictionary. Dieses bekomme ich auch nicht getrennt. Ziel des Ganzen wäre es z.B. einen String (conveyer__ip) bzw (conveyer__shuttle1__net_port) zu haben und das dazugehörige Value (12345) bzw. (13000). Ich hoffe ihr könnte mir ein wenig auf die Sprünge helfen.
LG

Code: Alles auswählen
#!/usr/bin/env python3
# coding: utf-8
import json
with open('data.json', 'r') as f:
data = json.load(f)
def flatten_json(y):
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + '_')
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
test = flatten_json(data)
print(test,sep=",")
print(test.get("conveyer1__shuttle1__net_port"))
Code: Alles auswählen
{'conveyer1__shuttle1__net_port': '13000', 'conveyer1__shuttle1__track1__right__node1_type': 'can', 'conveyer1__shuttle1__track1__right__node1_port': '0', 'conveyer1__shuttle1__track1__right__node1_id': '20', 'conveyer1__shuttle1__track1__right__node1_io1__dir': '1', 'conveyer1__shuttle1__track1__right__node1_io1__type': 'Stopper', 'conveyer1__shuttle1__track1__right__node1_io2__dir': '0', 'conveyer1__shuttle1__track1__right__node1_io2__type': 'Stopini', 'conveyer1__shuttle1__track1__right__node1_io3__dir': None, 'conveyer1__shuttle1__track1__right__node1_io3__type': None, 'conveyer1__shuttle1__track1__right__node1_io4__dir': None, 'conveyer1__shuttle1__track1__right__node1_io4__type': None}
13000
Code: Alles auswählen
{
"conveyer1": [{
"ip": "12345",
"shuttle1": [{
"net_port": "13000",
"track1": [{
"right": [{
"node1": {
"type": "can",
"port": "0",
"id": "20",
"io1": [{
"dir": "1",
"type": "Stopper"
}],
"io2": [{
"dir": "0",
"type": "Stopini"
}],
"io3": [{
"dir": null,
"type": null
}],
"io4": [{
"dir": null,
"type": null
}],
"motor1": [{
"dir": {},
"mode": {},
"speed": {},
"acc": {}
}]
}
}],
"left": [{
"node1": {
"type": {},
"port": {},
"id": {},
"io1": [{
"dir": {},
"type": {}
}],
"io2": [{
"dir": {},
"type": {}
}],
"io3": [{
"dir": {},
"type": {}
}],
"io4": [{
"dir": {},
"type": {}
}],
"motor2": [{
"dir": {},
"mode": {},
"speed": {},
"acc": {}
}]
}
}]
}]
}]
}]
}