[英]Python : Get value from a list of Json dictionary
I need to get values from google api response. 我需要从Google API响应中获取值。 I have json response as follows:
我有json响应,如下所示:
direct =
[{
'summary': 'Jakarta Inner Ring Road/Jl. Pantura/Jl. Tol Pelabuhan',
'legs': [{
'duration': {'value': 2130, 'text': '36 mins'},
'via_waypoint': [],
'start_address': 'North Jakarta, North Jakarta City, Special Capital Region of Jakarta, Indonesia',
'duration_in_traffic': {'value': 2475, 'text': '41 mins'},
'end_address': 'West Jakarta, West Jakarta City, Special Capital Region of Jakarta, Indonesia',
'start_location': {'lat': -6.138709899999999, 'lng': 106.8640136},
'distance': {'value': 17777, 'text': '17.8 km'},
'traffic_speed_entry': [],
'end_location': {'lat': -6.168346199999999, 'lng': 106.7588346}
}]
}]
I need to get the value
of duration
and distance
from legs
我需要获取
duration
和到legs
distance
的value
I've tried several ways, this is my last code: 我尝试了几种方法,这是我的最后一个代码:
for who in direct:
act_dist = (direct ["legs"][0]["distance"]["value"])
act_time2 = (direct2 ["legs"][0]["duration"]["value"])
and get this error: *Traceback (most recent call last): 并得到以下错误:* Traceback(最近一次通话):
File "C:src/distance.py", line 34, in <module>
act_dist = (direct ["legs"][0]["distance"]["value"])
TypeError: list indices must be integers or slices, not str*
Can anyone help with this problem? 任何人都可以帮助解决这个问题吗? Thanks
谢谢
Your code should actually be: 您的代码实际上应该是:
for who in direct:
act_dist = (who ["legs"][0]["distance"]["value"])
act_time2 = (who ["legs"][0]["duration"]["value"])
for d in direct:
for l in d['legs']:
print l['duration']
print l['distance']
You need to traverse all lists. 您需要遍历所有列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.