我在Python中操作一个URL,该URL调用API并在JSON中获得有效的结果。在
我只需要[result]提供的“纬度”和“经度”
但是,我似乎找不到任何好的方法来处理dict值的返回。在
我试图将JSON转换为一个字符串,但由于我的最终目标是将纬度和经度附加到另一个URL上,这看起来很傻吗?在
一直在四处搜索,似乎解析DICT是一个常见的问题。在
谁能给我指出正确的方向吗?在
我的代码(运行)
import urllib2
import json
Postcode = raw_input("Hello hello hello, what's your postcode..? ")
PostcodeURL = 'https://api.postcodes.io/postcodes/' + Postcode
json_str = urllib2.urlopen(PostcodeURL).read()
d = json.loads(json_str)
print(d)
输出如下:
^{pr2}$
您可以像字典一样访问JSON对象。因此,要从结果中获得经纬度,可以使用:
lat, long = d[result][latitude], d[result][longitude]
将它们加载到字符串中,进行错误检查,然后将它们附加到新的URL中。在
您有一个嵌套字典,因此使用正确的键访问它的值:
相关问题 更多 >
编程相关推荐