[英]How do you pull JSON array from API in Python?
I am having issues pulling a JSON array from http://api.divesites.com .我在从http://api.divesites.com拉出 JSON 阵列时遇到问题。 When I manually set the data as a string in Python 3.7, I can see the array.
当我在 Python 3.7 中手动将数据设置为字符串时,可以看到数组。
import json
def main():
hardcode = """{"request":{"str":null,"timestamp":1572865590,"loc":{"lat":"50.442","lng":"-4.08279999999999"},"mode":"sites","dist":"9","api":1},"sites":[{"currents":null,"distance":"7.47","hazards":null,"lat":"50.3378","name":"Fort Bovisand","water":null,"marinelife":null,"description":null,"maxdepth":null,"mindepth":null,"predive":null,"id":"24387","equipment":null,"lng":"-4.1285"},{"currents":null,"distance":"7.93","hazards":null,"lat":"50.3352","name":"Plymouth Breakwater","water":null,"marinelife":null,"description":null,"maxdepth":null,"mindepth":null,"predive":null,"id":"24388","equipment":null,"lng":"-4.1485"}],"version":1,"loc":{"lat":"50.442","lng":"-4.08279999999999"},"result":true}
"""
print("Original Data: ---", get_data(hardcode))
print("Site Data:----", get_data(hardcode)['sites'])
def get_data(data):
return json.loads(data)
if __name__=='__main__':
main()
Output Output
Original Data: --- {'request': {'str': None, 'timestamp': 1572865590, 'loc': {'lat': '50.442', 'lng': '-4.08279999999999'}, 'mode': 'sites', 'dist': '9', 'api': 1}, 'sites': [{'currents': None, 'distance': '7.47', 'hazards': None, 'lat': '50.3378', 'name': 'Fort Bovisand', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24387', 'equipment': None, 'lng': '-4.1285'}, {'currents': None, 'distance': '7.93', 'hazards': None, 'lat': '50.3352', 'name': 'Plymouth Breakwater', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24388', 'equipment': None, 'lng': '-4.1485'}], 'version': 1, 'loc': {'lat': '50.442', 'lng': '-4.08279999999999'}, 'result': True}
Site Data:---- [{'currents': None, 'distance': '7.47', 'hazards': None, 'lat': '50.3378', 'name': 'Fort Bovisand', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24387', 'equipment': None, 'lng': '-4.1285'}, {'currents': None, 'distance': '7.93', 'hazards': None, 'lat': '50.3352', 'name': 'Plymouth Breakwater', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24388', 'equipment': None, 'lng': '-4.1485'}]
However when I try and perform a GET request (using urllib.request.urlopen()
), I only receive the objects.但是,当我尝试执行 GET 请求(使用
urllib.request.urlopen()
)时,我只收到对象。
import urllib.request
import json
def get_jsonparsed_data(url):
response = urllib.request.urlopen(url)
data = response.read().decode("utf-8")
return json.loads(data)
def main():
url = 'http://api.divesites.com/?mode=sites&lat=-50.350874&lng=175.849890&dist=12'
print("Original Data: ---", get_jsonparsed_data(url))
print("Sites: ----", get_jsonparsed_data(url)['sites'])
if __name__=='__main__':
main()
Output Output
Original Data: --- {'request': {'str': None, 'timestamp': 1572866211, 'loc': {'lat': '-50.350874', 'lng': '175.849890'}, 'mode': 'sites', 'dist': '12', 'api': 1}, 'sites': [], 'version': 1, 'loc': {'lat': '-50.350874', 'lng': '175.849890'}, 'result': True}
Site Data:---- []
Am I doing something wrong, or do I need an extra step?我做错了什么,还是需要额外的步骤?
Solved it.解决了。 I spent so long focusing on debugging the code I forgot to check the final API call.
我花了很长时间专注于调试我忘记检查最终 API 调用的代码。 Turns out I either didn't have enough distance set, or the wrong starting longitude and latitude.
原来我要么没有设置足够的距离,要么是错误的起始经度和纬度。 This was different to my hard coded JSON.
这与我的硬编码 JSON 不同。
The URL should have been: http://api.divesites.com/?mode=sites&lat=50.442&lng=-4.08279999999999&dist=9 URL 应该是: http://api.divesites.com/?mode=sites&lat=50.442&lng=-4.08279999999999&dist=9
Stupid mistake.愚蠢的错误。 Thanks for helping though.
感谢您的帮助。 It got me to look.
它让我看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.