gpt4 book ai didi

Python - 从 JSON 响应中提取数据(使用 TomTom api)

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:28 24 4
gpt4 key购买 nike

我正在尝试使用 TomTom API 检索有关路线时间的信息.

通过提供 API url、API key ( that you can get by registering ) 以及源和目的地纬度/经度,我想返回行程时间。

我认为我正在从响应中获取字典的字典,并且应该能够使用 - jsonTomTomString['routes']['summary']['travelTimeInSeconds'] 访问数据

但我正在...

TypeError: list indices must be integers, not str

我的代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import ConfigParser
import requests
import json
import sys
import time
import datetime

from urllib import urlopen

# URL to the tomtom api
apiURL = "https://api.tomtom.com/routing/1/calculateRoute/"
# apiKey
apiKey = "get this from link in description"

#[coordinates]
sourceLat = 51.5560241
sourceLon = -0.2817075
destLat = 53.4630621
destLon = -2.2935288

tomtomURL = "%s/%s,%s:%s,%s/json?key=%s" % (apiURL,sourceLat,sourceLon,destLat,destLon,apiKey)

getData = urlopen(tomtomURL).read()
jsonTomTomString = json.loads(getData)

totalTime = jsonTomTomString['routes']['summary']['totalTimeSeconds']

print ("time to destination is: ", totalTime)

JSON 响应如下所示...

{"formatVersion":"0.0.12","copyright":"Copyright 2018 TomTom International BV. All rights reserved. This navigation data is the proprietary copyright of TomTom International BV and may be used only in accordance with the terms of a fully executed license agreement entered into between TomTom International BV, or an authorised reseller and yourself. If you have not entered into such a license agreement you are not authorised to use this data in any manner and should immediately return it to TomTom International BV.","privacy":"TomTom keeps information that tells us how and when you use our services. This includes information about the device you are using and the information we receive while you use the service, such as locations, routes, destinations and search queries. TomTom is unable to identify you based on the information it collects, and will not try to. TomTom uses the information for technical diagnostics, to detect fraud and abuse, to create usage reports, and to improve its services. The information is kept only for these purposes and for a limited period of time, after which it is destroyed. TomTom applies security methods based on industry standards to protect the information against unauthorised access. TomTom will not give anyone else access to the information or use it for any other purpose, unless explicitly and lawfully ordered to do so following due legal process. You can find out more at http://tomtom.com/privacy. You can contact TomTom by going to http://tomtom.com/support.","routes":[{"summary":{"lengthInMeters":326856,"travelTimeInSeconds":13018,"trafficDelayInSeconds":818,"departureTime":"2018-01-08T17:10:31Z","arrivalTime":"2018-01-08T20:47:28Z"},"legs":[{"summary":{"lengthInMeters":326856,"travelTimeInSeconds":13018,"trafficDelayInSeconds":818,"departureTime":"2018-01-08T17:10:31Z","arrivalTime":"2018-01-08T20:47:28Z"},"points":[{"latitude":51.55598,"longitude":-0.28216},{"latitude":51.55601,"longitude":-0.28292},{"latitude":51.55602,"longitude":-0.28384},{"latitude":51.55602,"longitude":-0.28395},{"latitude":51.55602,"longitude":-0.28442},{"latitude":51.55603,"longitude":-0.28493},{"latitude":51.55603,"longitude":-0.28564},{"latitude":51.55603,"longitude":-0.28611},

感谢您帮助实现此功能。

最佳答案

我在"routes":后面看到一个[,所以routes是一个数组。

尝试 jsonTomTomString['routes'][0]['summary']['totalTimeSeconds']

关于Python - 从 JSON 响应中提取数据(使用 TomTom api),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48155038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com