使用simplejson在json文件中返回5个深度的值

时间:2015-06-27 10:38:25

标签: 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'

到目前为止,这是我的代码;

import urllib, simplejson
url = "http://131940.qld.gov.au/api/json/v1/events/?state=qld"
response = urllib.urlopen(url);
data = simplejson.loads(response.read())

for data['features'][0]['properties'] in data['features']:
    print data['features'][0]['properties']['location']['road']

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
        }
      }
    },

我尝试了print data['features'][0]['properties']['location']['road']的多种组合,例如。在['properties']之后尝试不同的索引或标记名称变体,但没有成功。

有关如何阅读JSON文件的任何建议/代码示例,以及如何构建字典/列表的解释将非常感激。

1 个答案:

答案 0 :(得分:0)

您应该将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']