字典长度等于3,但是当尝试访问接收到KeyError

时间:2019-07-05 21:50:49

标签: python list dictionary

我正在尝试解析如下所示的json响应:

{
"links": {
    "next": "http://www.neowsapp.com/rest/v1/feed?start_date=2015-09-08&end_date=2015-09-09&detailed=false&api_key=xxx",
    "prev": "http://www.neowsapp.com/rest/v1/feed?start_date=2015-09-06&end_date=2015-09-07&detailed=false&api_key=xxx",
    "self": "http://www.neowsapp.com/rest/v1/feed?start_date=2015-09-07&end_date=2015-09-08&detailed=false&api_key=xxx"
},
"element_count": 22,
"near_earth_objects": {
    "2015-09-08": [
        {
            "links": {
                "self": "http://www.neowsapp.com/rest/v1/neo/3726710?api_key=xxx"
            },
            "id": "3726710",
            "neo_reference_id": "3726710",
            "name": "(2015 RC)",
            "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3726710",
            "absolute_magnitude_h": 24.3,
            "estimated_diameter": {
                "kilometers": {
                    "estimated_diameter_min": 0.0366906138,
                    "estimated_diameter_max": 0.0820427065
                },
                "meters": {
                    "estimated_diameter_min": 36.6906137531,
                    "estimated_diameter_max": 82.0427064882
                },
                "miles": {
                    "estimated_diameter_min": 0.0227984834,
                    "estimated_diameter_max": 0.0509789586
                },
                "feet": {
                    "estimated_diameter_min": 120.3760332259,
                    "estimated_diameter_max": 269.1689931548
                }
            },
            "is_potentially_hazardous_asteroid": false,
            "close_approach_data": [
                {
                    "close_approach_date": "2015-09-08",
                    "close_approach_date_full": "2015-Sep-08 09:45",
                    "epoch_date_close_approach": 1441705500000,
                    "relative_velocity": {
                        "kilometers_per_second": "19.4850295284",
                        "kilometers_per_hour": "70146.106302123",
                        "miles_per_hour": "43586.0625520053"
                    },
                    "miss_distance": {
                        "astronomical": "0.0269230459",
                        "lunar": "10.4730648551",
                        "kilometers": "4027630.320552233",
                        "miles": "2502653.4316094954"
                    },
                    "orbiting_body": "Earth"
                }
            ],
            "is_sentry_object": false
        },

}

我试图弄清楚如何解析以获得“ miss_distance”字典值?我无法把头缠住它。

这是我到目前为止所能做的:

  1. 从request.get()获得响应对象后 响应= request.get(URL

  2. 我将响应对象转换为json对象 data = response.json()#返回字典对象

  3. 我尝试解析字典的第一级: 对于我的数据:   如果我==“ near_earth_objects”:     数据集1 =数据[“ near_earth_objects”] [“ 2015-09-08”]     #this返回下一个列表类型的对象

请有人可以向我解释: 1.首先如何解密此响应。 2.如何继续解析响应对象并进入miss_distance词典?

请提供任何指针/帮助。

谢谢

2 个答案:

答案 0 :(得分:1)

您的数据将在每个日期,近地物体和近距离方法中包含多个词典:

near_earth_objects = data['near_earth_objects']
for date in near_earth_objects:
    objects = near_earth_objects[date]
    for object in objects:
        close_approach_data = object['close_approach_data']
        for close_approach in close_approach_data:
            print(close_approach['miss_distance'])

答案 1 :(得分:0)

下面的代码为您提供了每个日期的每个对象的date, miss_distances

import json
raw_json = '''
{
    "near_earth_objects": {
        "2015-09-08": [
            {
                "close_approach_data": [
                    {
                        "miss_distance": {
                            "astronomical": "0.0269230459",
                            "lunar": "10.4730648551",
                            "kilometers": "4027630.320552233",
                            "miles": "2502653.4316094954"
                        },
                        "orbiting_body": "Earth"
                    }
                ]
            }
        ]
    }
}
'''

if __name__ == "__main__":
    parsed = json.loads(raw_json)

    # assuming this json includes more than one near_earch_object spread across dates
    near_objects = []
    for date, near_objs in parsed['near_earth_objects'].items():
        for obj in near_objs:
            for appr in obj['close_approach_data']:
                o = {
                    'date': date,
                    'miss_distances': appr['miss_distance']
                }
                near_objects.append(o)
    print(near_objects)

输出:

[
  {'date': '2015-09-08', 
   'miss_distances': {
     'astronomical': '0.0269230459', 
     'lunar': '10.4730648551', 
     'kilometers': '4027630.320552233', 
     'miles': '2502653.4316094954'
    }
  }
]