在python中把response.text转换为字典

4 人不认可

我有一个API,它在response.text中给我提供了以下数据,我怎样才能将这些数据转换为一个python字典?

response.text

[{"_id":"5dccedadff47e867a2833819","tel":"XXXX","loc":[28.498692,77.095215],"tripId":"5dccedaaff47e867a28337ec","mode":"automated","osm_data":{"distance_remained":10791,"time_remained":1649.5},"distance_remained":10870,"time_remained":1173,"curr_ETA":"2019-11-14T06:43:19.664Z","address":"100,
The National Media Centre, Sector 24, Gurugram, Haryana 122022,
India","city":"Gurugram","createdAt":"2019-11-14T06:01:17.166Z"},{"_id":"5dccedacff47e867a2833801","tel":"XXXX","loc":[28.498692,77.095215],"tripId":"5dccedaaff47e867a28337ec","mode":"automated","osm_data":{"distance_remained":10791,"time_remained":1649.5},"distance_remained":10870,"time_remained":1173,"curr_ETA":"2019-11-14T06:43:18.459Z","address":"100,
The National Media Centre, Sector 24, Gurugram, Haryana 122022,
India","city":"Gurugram","createdAt":"2019-11-14T06:01:16.163Z"}]

我想以一个字典的形式访问这个 response.text 中的数据。

3 个评论
response.json 会给你一个字典的列表,假设响应是json编码的。
这是一个很有帮助的问题,得到了支持。
python
django
Rahul Sharma
Rahul Sharma
发布于 2019-11-14
2 个回答
Daniel Hepper
Daniel Hepper
发布于 2019-11-14
已采纳
0 人赞同

这看起来像一个JSON字符串。你可以用 json.loads() 来解析它。

>>> import json
>>> json.loads(response.text)

假设你所说的response来自于使用requests库,你也可以简单地做response.json()

vgp2018
vgp2018
发布于 2019-11-14
0 人赞同

This is simple:

import json
resp_text = request.text