使用simplejson在python的5级json文件中返回值

使用simplejson在python的5级json文件中返回值,python,json,simplejson,Python,Json,Simplejson,尝试从JSON文件中获取值,在python中可以达到5个级别。当前正在获取此错误 Traceback (most recent call last): File "traffic.py", line 9, in <module> print data['features'][0]['properties']['location']['road'] KeyError: 'location' JSON文件的片段 { "type": "FeatureCollection", "p

尝试从JSON文件中获取值,在python中可以达到5个级别。当前正在获取此错误

Traceback (most recent call last):
File "traffic.py", line 9, in <module>
print data['features'][0]['properties']['location']['road']
KeyError: 'location'
JSON文件的片段

{
  "type": "FeatureCollection",
  "published": "27/06/2015 19:15",
  "rights": {
    "owner": "Department of Transport and Main Roads",
    "disclaimer": "The State of Queensland makes no statements, representations or warranties about the accuracy, currency, reliability or completeness of the information contained in this feed.",
    "copyright": "Copyright in material within this feed is owned by the State of Queensland or other entities which provide material for the website by arrangement. Please consult the 131940 website for further information."
  },
  "features": [
    {
      "type": "Feature",
      "id": 55141891,
      "source": {
        "externalid": null,
        "id": 2,
        "name": "Department of Transport and Main Roads"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          152.356430053711,
          -27.5912036895752
        ],
        "classifier": null
      },
      "properties": {
        "location": {
          "road": "VICTORIA STREET",
          "suburb": "FOREST HILL",
          "localGovernment": null,
          "postcode": "4342",
          "region": {
            "id": 104,
            "name": "Darling Downs"
          },
          "state": "QLD",
          "direction": "Both Directions",
          "additional": "Gallipoli Rememberence March.\r\nVictoria Street Forest Hill, between Church Street and William Steet"
        },
        "event": {
          "isHighImpact": false,
          "description": "Special Event Both Directions\r\nGallipoli Rememberence March.\r\nVictoria Street Forest Hill, between Church Street and William Steet.\r\nCommencing: 22 August 2015, between the hours of 10:30am and 11:15am\r\nConcluding: 22 August 2015.\r\nRoad closed both directions\r\nNo delays expected. Use alternative route. \r\n\r\n\r\nLast edited: 08 May 2015 09:35",
          "type": "Planned",
          "cause": "Special Event",
          "incidentType": "",
          "incidentDetails": "N/A",
          "delay": "No delays expected",
          "advice": "Use alternative route",
          "limit": null,
          "extraDetails": "\r\n",
          "impact": null
        },
        "temporal": {
          "start": "22/08/2015T10:30:00",
          "modified": "08/05/2015T09:35:14",
          "end": null,
          "review": null,
          "lastReviewed": null,
          "nextUpdate": null
        },
        "metadata": {
          "status": null,
          "owner": null,
          "modifiedBy": null,
          "url": "http://131940.qld.gov.au/road-conditions.aspx?id=55141891",
          "contactEmail": null
        }
      }
    },
我尝试了多种组合的
打印数据['features'][0]['properties']['location']['road']
例如,在['properties']之后尝试索引或标记名的不同变体,但没有成功


非常感谢您提供有关如何将这些内容读入JSON文件的任何建议/代码示例,以及解释字典/列表的结构。

您应该将
for
循环更改为:

for info in data['features']:
    print info['properties']['location']['road']
说明:
data['features']
返回字典列表,其中每个字典如下所示:

{u'geometry': {u'type': u'Point', u'classifier': None, u'coordinates': [152.356430053711, -27.5912036895752]}, u'source': {u'externalid': None, u'id': 2, u'name': u'Department of Transport and Main Roads'}, u'type': u'Feature', u'id': 55141891, u'properties': {u'temporal': {u'end': None, u'nextUpdate': None, u'lastReviewed': None, u'modified': u'08/05/2015T09:35:14', u'start': u'22/08/2015T10:30:00', u'review': None}, u'metadata': {u'status': None, u'owner': None, u'contactEmail': None, u'modifiedBy': None, u'url': u'http://131940.qld.gov.au/road-conditions.aspx?id=55141891'}, u'location': {u'direction': u'Both Directions', u'additional': u'Gallipoli Rememberence March.\r\nVictoria Street Forest Hill, between Church Street and William Steet', u'region': {u'id': 104, u'name': u'Darling Downs'}, u'localGovernment': None, u'suburb': u'FOREST HILL', u'state': u'QLD', u'postcode': u'4342', u'road': u'VICTORIA STREET'}, u'event': {u'impact': None, u'description': u'Special Event Both Directions\r\nGallipoli Rememberence March.\r\nVictoria Street Forest Hill, between Church Street and William Steet.\r\nCommencing: 22 August 2015, between the hours of 10:30am and 11:15am\r\nConcluding: 22 August 2015.\r\nRoad closed both directions\r\nNo delays expected. Use alternative route. \r\n\r\n\r\nLast edited: 08 May 2015 09:35', u'type': u'Planned', u'advice': u'Use alternative route', u'delay': u'No delays expected', u'incidentDetails': u'N/A', u'limit': None, u'incidentType': u'', u'extraDetails': u'\r\n', u'cause': u'Special Event', u'isHighImpact': False}}}

因此,我们现在所需要做的就是进入已经奏效的
['properties']['location']['road']

,谢谢,这更简洁了。问题是具体在循环的构造方式上,还是我应该能够使用原始代码访问那么深的标记@DeepSpace@scl_84问题是您在错误的位置使用了错误的键(json只是Python字典的一个漂亮名称;)),这就是为什么会出现
KeyError
。在这种情况下,我的建议是在你通过时打印每个级别,直到你得到所需的数据。
{u'geometry': {u'type': u'Point', u'classifier': None, u'coordinates': [152.356430053711, -27.5912036895752]}, u'source': {u'externalid': None, u'id': 2, u'name': u'Department of Transport and Main Roads'}, u'type': u'Feature', u'id': 55141891, u'properties': {u'temporal': {u'end': None, u'nextUpdate': None, u'lastReviewed': None, u'modified': u'08/05/2015T09:35:14', u'start': u'22/08/2015T10:30:00', u'review': None}, u'metadata': {u'status': None, u'owner': None, u'contactEmail': None, u'modifiedBy': None, u'url': u'http://131940.qld.gov.au/road-conditions.aspx?id=55141891'}, u'location': {u'direction': u'Both Directions', u'additional': u'Gallipoli Rememberence March.\r\nVictoria Street Forest Hill, between Church Street and William Steet', u'region': {u'id': 104, u'name': u'Darling Downs'}, u'localGovernment': None, u'suburb': u'FOREST HILL', u'state': u'QLD', u'postcode': u'4342', u'road': u'VICTORIA STREET'}, u'event': {u'impact': None, u'description': u'Special Event Both Directions\r\nGallipoli Rememberence March.\r\nVictoria Street Forest Hill, between Church Street and William Steet.\r\nCommencing: 22 August 2015, between the hours of 10:30am and 11:15am\r\nConcluding: 22 August 2015.\r\nRoad closed both directions\r\nNo delays expected. Use alternative route. \r\n\r\n\r\nLast edited: 08 May 2015 09:35', u'type': u'Planned', u'advice': u'Use alternative route', u'delay': u'No delays expected', u'incidentDetails': u'N/A', u'limit': None, u'incidentType': u'', u'extraDetails': u'\r\n', u'cause': u'Special Event', u'isHighImpact': False}}}