【问题标题】:How to get location from get value json in python?如何从python中的get value json获取位置?
【发布时间】:2016-02-24 06:56:32
【问题描述】:

如何通过 Python 在文件 json 中使用来自 lat/lng 的反向地理编码。之后打印文件 json 位置名称。

import json

Json_data=[{"lat":"3.160","lng":"101.710"},
           {"lat":"2.350","lng":"102.030"},
           {"lat":"6.120","lng":"102.130"}]

#Json_data=[{"lat":3.160,"lng":101.710},
#           {"lat":2.350,"lng":102.030},
#           {"lat":6.120,"lng":102.130}]

def revrseGeocode(latlng):
    result={}
    url ='https://maps.googleapis.com/maps/api/geocode/json?latlng={0} &key={1}'
    apikey='AIzaSyC6gSdW2pWz9QgaBY2ZlZcYRTKva9-x74w'
    request= url.format(latlng,apikey)
    data= json.loads(request)
    if len(data['results'])>0:
        result=data['results'][0]
        return result

for i,row in Json_data:
    Json_data[i]= revrseGeocode(Json_data[1][i]+','+Json_data[1][i])

for i,row in Json_data:
    if 'address_components' in row['geocode_data']:
        for component in row['geocode_data']['address_components']:
            if 'country' in component['types']:
                Json_data['country'][i]= component['long_name']
        for component in row['geocode_data']['address_components']:
            if 'locality' in component['types']:
                Json_data['city'][i]=component['long_name']
                break
            elif 'postal_town' in component['types']:
                Json_data['city'][i] = component['long_name']
                break
            elif 'administrative_area_level_2' in component['types']:
                Json_data['city'][i]=component['long_name']
                break
            elif 'administrative_area_level_1' in component['types']:
                Json_data['city'][i]= component['long_name']
                break

#ERROR:

文件“/home/magic/Desktop/python/test1.py”,第 22 行,在 Json_data[i]= revrseGeocode(Json_data[1][i]+','+Json_data[1][i])

文件“/home/magic/Desktop/python/test1.py”,第 16 行,在 revrseGeocode 数据= json.loads(请求)

文件“/usr/lib/python2.7/json/init.py”,第 338 行,加载中 返回_default_decoder.decode(s)

文件“/usr/lib/python2.7/json/decoder.py”,第 366 行,在解码中 obj, end = self.raw_decode(s, idx=_w(s, 0).end())

文件“/usr/lib/python2.7/json/decoder.py”,第 384 行,在 raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded

【问题讨论】:

  • “反向地理编码”是什么意思?请提供详细信息。至少,说明预期的输出是什么。
  • 你在研究这个时发现了什么?
  • 您尝试将列表视为字典

标签: python json google-maps


【解决方案1】:

首先,您的 Json_data 是一个列表。您可以通过 Jason_data[index] 访问它,其中 index 可以是 0 到 len(Jason_data) 之间的任何数字。

您正在尝试将其作为字典进行访问。因此出现错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 2013-12-27
    • 2023-03-28
    • 1970-01-01
    • 2022-12-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多