简体   繁体   中英

nested JSON with python issues

So I', not even sure if I'm asking the right question. What I need to pull is lat, long and flight number. I can print/pull the entire bit of a field/set called geography, and the entire bit of aircraft, but pulling just lat or long I'm hitting a wall. The JSON snippet

[
    {
        "geography": {
            "latitude": "27.3200",
            "longitude": "50.5700",
            "altitude": 12496.8,
            "direction": "144.00"
        },
        "speed": {
            "horizontal": 1003.784,
            "isGround": 0,
            "vertical": 0
        },
.... (several other chunks like this, one per aircraft...)
}]

the code I'm trying to use..

def json_filter(airline):
    with open('geo/'+ airline +'.json') as f:
      data = json.load(f)
      x = len(data)
      print(x)
      for d in range(0,x):
            #print (data[d]["aircraft"])
            print (data[d]["iataNumber"])
            print (data[d]["latitude"])
            print (data[d]["longitude"])

Am I on the right track or could someone be so kind as to help me out or explain what's wrong?

You can do like this... data[d] access only a single object and geography is ana attribute so, you have to access it also by subscripting.

def json_filter(airline):
with open('geo/'+ airline +'.json') as f:
  data = json.load(f)
  x = len(data)
  print(x)
  for d in range(0,x):
        print (data[d]["geography"]["latitude"])
        print (data[d]["speed"]["horizontal"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM