【问题标题】:Parsing coordinates from JSON in a loop在循环中解析来自 JSON 的坐标
【发布时间】:2021-02-22 17:34:37
【问题描述】:

我正在解析来自 JSON 对象的纬度和经度坐标。

目前我正在这样做

    tenMileRadius = 'http://chargepoints.dft.gov.uk/api/retrieve/registry/lat/' + str(latitude) + '/long/' + str(longitude) + '/dist/10/format/json'

    # GET The 10 closest devices to the given postcode
    response = requests.get(tenMileRadius).json()
    
    alat = response['ChargeDevice'][0]['ChargeDeviceLocation']['Latitude']
    blat = response['ChargeDevice'][1]['ChargeDeviceLocation']['Latitude']
    clat = response['ChargeDevice'][2]['ChargeDeviceLocation']['Latitude']
    dlat = response['ChargeDevice'][3]['ChargeDeviceLocation']['Latitude']
    elat = response['ChargeDevice'][4]['ChargeDeviceLocation']['Latitude']
    flat = response['ChargeDevice'][5]['ChargeDeviceLocation']['Latitude']
    glat = response['ChargeDevice'][6]['ChargeDeviceLocation']['Latitude']
    hlat = response['ChargeDevice'][7]['ChargeDeviceLocation']['Latitude']
    ilat = response['ChargeDevice'][8]['ChargeDeviceLocation']['Latitude']
    jlat = response['ChargeDevice'][9]['ChargeDeviceLocation']['Latitude']

    alon = response['ChargeDevice'][0]['ChargeDeviceLocation']['Longitude']
    blon = response['ChargeDevice'][1]['ChargeDeviceLocation']['Longitude']
    clon = response['ChargeDevice'][2]['ChargeDeviceLocation']['Longitude']
    dlon = response['ChargeDevice'][3]['ChargeDeviceLocation']['Longitude']
    elon = response['ChargeDevice'][4]['ChargeDeviceLocation']['Longitude']
    flon = response['ChargeDevice'][5]['ChargeDeviceLocation']['Longitude']
    glon = response['ChargeDevice'][6]['ChargeDeviceLocation']['Longitude']
    hlon = response['ChargeDevice'][7]['ChargeDeviceLocation']['Longitude']
    ilon = response['ChargeDevice'][8]['ChargeDeviceLocation']['Longitude']
    jlon = response['ChargeDevice'][9]['ChargeDeviceLocation']['Longitude']

但是我意识到这是不雅且低级别的,并且返回的坐标数会有所不同 - 这种方法会错过。如何通过遍历 JSON 来访问坐标,以便我拥有这种格式的坐标 -

coords = [[latitude, longitude],
    [latitude, longitude],
    [latitude, longitude],
    [latitude, longitude],
    [latitude, longitude]]

JSON 看起来像这样

   {'Scheme':
    {'SchemeCode': 'NA', 'SchemeData':
    {'OrganisationName': 'NA', 'Website': 'NA', 'TelephoneNo': 'NA'}},
    'ChargeDevice': [{'ChargeDeviceId': 'bc783b8d7776bd472c216a4672883f8b',
    'ChargeDeviceLocation': {'Latitude': '55.326762', 'Longitude': '-1.582318', ...
  {'ChargeDeviceId': 'f39dfe556bde4e399ff9df50170b13b6', 'ChargeDeviceRef': 'GP11934'...
    'ChargeDeviceLocation': {'Latitude': '52.554362', 'Longitude': '-1.382325' ...
{'ChargeDeviceId': '179ddc389a9324cebba038fb4ac783d5', 'ChargeDeviceRef': 'GP11960', ....
    'ChargeDeviceLocation': {'Latitude': '52.765253', 'Longitude': '-0.881864'....

【问题讨论】:

  • 你能发布一个示例 json 文件吗,似乎无法访问该链接。
  • 我在我的问题中添加了一个显示 JSON 结构的编辑部分。

标签: python json loops


【解决方案1】:

您可以使用列表推导来提取所有纬度/经度坐标。

coordinates = [
[device['chargeDeviceLocation']['latitude'],
 device['chargeDeviceLocation']['longitude']] \
for device in response['chargeDevice']]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多