获取嵌套 JSON 字典中的值索引以检索 Python 中的同级值

Getting index of value in nested JSON dictionary to retrieve sibling values in Python

我首先将 XML 转换为 JSON。

我有一个嵌套值,我想找到它的索引以便检索它的同级字典。这些元素在同一个父元素下 Location。下面的JSON是删节版,还有很多Location部词典。

例如,从提供值New York到:
Location > LocationMetaData > LocationName

我要检索:
Location > LocationData > TimeData > Period > DateTimeTo
Location > LocationData > TimeData > Element > ElementIndex

我被困在这里,正在尝试以下方法:

json_d['Root']['Location'][0]['LocationMetaData']['LocationName']

JSON

{
  "Root":{
    "Origin":{
      "Firstname":"Guy",
      "Lastname":"Man"
    },
    "Identification":{
      "Title":"The Title",
      "DateTime":"2022-05-26 09:00"
    },
    "Location":[
      {
        "LocationMetaData":{
          "LocationId":"192",
          "LocationName":"New York"
        },
        "LocationData":{
          "TimeData":[
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 12:00",
                "DateTimeTo":"2022-05-26 13:00"
              },
              "Element":{
                "ElementValue":"V",
                "ElementIndex":"9"
              }
            },
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 13:00",
                "DateTimeTo":"2022-05-26 14:00"
              },
              "Element":{
                "ElementValue":"V",
                "ElementIndex":"8"
              }
            },
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 14:00",
                "DateTimeTo":"2022-05-26 15:00"
              },
              "Element":{
                "ElementValue":"H",
                "ElementIndex":"6"
              }
            }
          ]
        }
      }
    ],
    "Location":[
      {
        "LocationMetaData":{
          "LocationId":"168",
          "LocationName":"Chicago"
        },
        "LocationData":{
          "TimeData":[
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 12:00",
                "DateTimeTo":"2022-05-26 13:00"
              },
              "Element":{
                "ElementValue":"V",
                "ElementIndex":"9"
              }
            },
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 13:00",
                "DateTimeTo":"2022-05-26 14:00"
              },
              "Element":{
                "ElementValue":"V",
                "ElementIndex":"8"
              }
            },
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 14:00",
                "DateTimeTo":"2022-05-26 15:00"
              },
              "Element":{
                "ElementValue":"H",
                "ElementIndex":"6"
              }
            }
          ]
        }
      }
    ]
  }
}

Python

#!/usr/bin/env python3

import xmltodict
import json
import requests

url = "https://example.com/file.xml"
xml = requests.get(url)

json_s = json.dumps(xmltodict.parse(xml.content), indent=2)
#print(json_s)

#dict
json_d = json.loads(json_s)

#need to find index by providing value of LocationName
json_d = json_d['Root']['Location'][0]
city = json_d['LocationMetaData']['LocationName'] #new york

count = 0

for key in json_d['LocationData']['TimeData']:
    timeto = key['Root']['DateTimeTo'] 
    indx = key['Element']['ElementIndex']
    level = key['Element']['ElementValue']
    level = {
        'L': 'Low',
        'M': 'Medium',
        'H': 'High',
        'V': 'Very High',
        'E': 'Extreme'} [level]
    print(f'{timeto}\t{indx} ({level})')
    count += 1
    if count == 15: break

如果 json_d 是您解析的 json 文件,您可以尝试:

for location in json_d["Root"]["Location"]:
    city = location["LocationMetaData"]["LocationName"]
    date_time_to = []
    element_idx = []
    element_values = []
    for td in location["LocationData"]["TimeData"]:
        date_time_to.append(td["Period"]["DateTimeTo"])
        element_idx.append(td["Element"]["ElementIndex"])
        element_values.append(td["Element"]["ElementValue"])

    print(city)
    print("-" * 80)
    for t, e, v in zip(date_time_to, element_idx, element_values):
        print(f"{t} - {e} - {v}")
    print()

打印:

Chicago
--------------------------------------------------------------------------------
2022-05-26 13:00 - 9 - V
2022-05-26 14:00 - 8 - V
2022-05-26 15:00 - 6 - H