Seite 1 von 1

Mit Python JSON Parsen

Verfasst: Dienstag 16. April 2019, 14:54
von Kirikkayis
Moin,

hab folgende JSON-Datei:

Code: Alles auswählen

{
  "generalConfig": [
    {
      "name": "Letzte \u00c4nderung",
      "background-color": "",
      "flag": "Qt::ItemIsEditable",
      "currentValue": {
        "currentValue": "25.01.2019  10:25",
        "background-color": "#6E6E6E",
        "flag": "",
        "role": "InputField",
        "valid": "(\\d+[.,]?\\d+)"
      },
      "lowerTolerance": {
        "currentValue": "",
        "background-color": "#EFE9E9",
        "flag": "",
        "role": "InputField"
      },
      "upperTolerance": {
        "currentValue": "",
        "background-color": "#EFE9E9",
        "flag": "",
        "role": "InputField"
      },
      "status": {
        "currentValue": "",
        "background-color": "#EFE9E9",
        "flag": "",
        "role": "InputField"
      }
    },
    {
      "name": "Solldrehzahl",
      "background-color": "",
      "flag": "Qt::ItemIsEditable",
      "currentValue": {
        "currentValue": 1200.0,
        "background-color": "#6E6E6E",
        "flag": "",
        "role": "InputField",
        "valid": "(\\d+[.,]?\\d+)"
      },
      "lowerTolerance": {
        "currentValue": "",
        "background-color": "#EFE9E9",
        "flag": "",
        "role": "InputField"
      },
      "upperTolerance": {
        "currentValue": "",
        "background-color": "#EFE9E9",
        "flag": "",
        "role": "InputField"
      },
      "status": {
        "currentValue": "",
        "background-color": "#EFE9E9",
        "flag": "",
        "role": "InputField"
      }
    },
    {
      "name": "Abstand a",
      "background-color": "",
      "flag": "Qt::ItemIsEditable",
      "currentValue": {
        "currentValue": "5.0",
        "background-color": "#6E6E6E",
        "flag": "",
        "role": "InputField",
        "valid": "(\\d+[.,]?\\d+)"
      },
      "lowerTolerance": {
        "currentValue": "",
        "background-color": "#EFE9E9",
        "flag": "",
        "role": "InputField"
      },
      "upperTolerance": {
        "currentValue": "",
        "background-color": "#EFE9E9",
        "flag": "",
        "role": "InputField"
      },
      "status": {
        "currentValue": "",
        "background-color": "#EFE9E9",
        "flag": "",
        "role": "InputField"
      }
    }
    .
    .
    .
    .
    .
würde jetzt gerne über diese JSON-Datei durch "iterieren" und einmal in "status" --> "background-color" auslesen und einmal in "currentValue" --> "currentValue" auslesen.

Bis jetzt habe ich folgenden Code:

Code: Alles auswählen


import json
from pprint import pprint


with open('newmain.json', 'r') as f:
    distros_dict = json.load(f)

print(distros_dict["generalConfig"])

Soweit funktioniert es auch einwandfrei ...
nur weis ich jetzt leider nicht wie ich auf "status" bzw. "currentValue" zugreifen kann ...
Jemand eine Idee?

Re: Mit Python JSON Parsen

Verfasst: Dienstag 16. April 2019, 14:58
von __deets__
Indem du ueber die Werte von generalConfig iterierst, und dann wiederum auf die verschiedenen Schluessel der enthaltenen Objekte zugreifst.

Re: Mit Python JSON Parsen

Verfasst: Dienstag 16. April 2019, 15:03
von Kirikkayis
Danke für die schnelle Antwort.

Ich habe leider keine Ahnung wie ich jetzt nun eine Ebene Tiefer gehen kann ...
habe folgendes Probiert:

Code: Alles auswählen


from json_logic import jsonLogic
import json
from pprint import pprint


with open('newmain.json', 'r') as f:
    distros_dict = json.load(f)

print(json.dumps(distros_dict["generalConfig"]["name"]))

Fehlermeldung:
Exception has occurred: TypeError
list indices must be integers or slices, not str

Re: Mit Python JSON Parsen

Verfasst: Dienstag 16. April 2019, 15:08
von __deets__
Hast du dir mal ausgegeben, was das fuer ein Objekt ist das du mit distros_dict["generalConfig"] erhaelst? Und was glaubst du kann das damit zu tun haben, dass du mit dem "name" nicht ans Ziel kommst? Denn ich sehe "name" kommt wenigstens 3-mal vor. Was wuerdest du denn erwarten, was du da bekommst?