我一直在从事计算机科学30课程的任务。在这项任务中,我被要求访问API并返回一个不仅仅是整个字典的特定值。我的问题是我似乎无法打印我想要的值,因为它是一个字符串而不是整数或切片。如果您决定向我提供帮助,这是我的完整代码。谢谢你的时间。
import requests
import urllib.parse
# set up a temporary copy of the google api
temp = "https://maps.googleapis.com/maps/api/directions/json?
origin=Disneyland&destination=Universal+Studios+Hollywood4"
# set up the main API and the origin and destination inputs from the user
main_api = "https://maps.googleapis.com/maps/api/directions/json?"
origin = str(input("Enter in the place that is you are going away from: "))
destination = str(input("Enter the destination that you would like to reach:
"))
# set up the url from the main api, the origin, and the destination
url = main_api + urllib.parse.urlencode({"origin": origin, "destination":
destination})
# print the url back to the user
print(url)
# show the user the distance between the origin and the destination
json_data = requests.get(url).json()
print(json_data['routes']['legs']['distance']['text'])
答案 0 :(得分:0)
你有几个嵌套在json dict响应中的列表。检查以下结果!
class Test from React.Component {
getPlainText = () => {
const plainText = /* some method */(this.renderParagraph())
console.log(plainText) // output in console: <p>I'm text from props of Text component!</p>
}
renderParagraph () {
return <p>{this.props.text}</p>
}
render () {
return <button onClick={this.getPlainText}>click me will print {this.props.text} in console</button>
}
}
输出
json_data['routes'][0]['legs'][0]['distance']