我对存储在以下变量中的get请求有一些响应...
dashboard = 'http://12.345.67.890:8000/api/search?query=&starred=false'
dashboardr = s.get(dashboard)
dashboards = dashboardr.content
print(dashboards)
响应看起来如下...
[{"id":19,"title":"Apple","uri":"db/abc-api","type":"dash-db","tags":[],"isStarred":false},{"id":20,"title":"Banana","uri":"db/cde-api","type":"dash-db","tags":[],"isStarred":false},{"id":7,"title":"Mango","uri":"db/efg","type":"dash-db","tags":[],"isStarred":false}]
有人可以帮我提取标题值并存储在另一个变量中吗?
上述响应中的标题值为
Apple
Banana
Mango
答案 0 :(得分:1)
5
您可以将其保存在列表变量中,而不是打印标题。
答案 1 :(得分:1)
使用eval(仪表板)代替仪表板。
dashboard = 'http://12.345.67.890:8000/api/search?query=&starred=false'
dashboardr = s.get(dashboard)
# eval() will convert a string to a python statement/expression
dashboards = eval(dashboardr.content)
title_list = []
for _ in dashboards:
title_list.append(dashboards["title"])
答案 2 :(得分:1)
假设您从HTTP调用获得的响应是一个字符串,则下面的代码提取标题。
import json
response_str = '[{"id": 19, "title": "Apple", "uri": "db/abc-api", "type": "dash-db", "tags": [], "isStarred": false},{"id": 20, "title": "Banana", "uri": "db/cde-api", "type": "dash-db", "tags": [], "isStarred": false},{"id": 7, "title": "Mango", "uri": "db/efg", "type": "dash-db", "tags": [], "isStarred": false}]'
response_dict = json.loads(response_str)
titles = [entry['title'] for entry in response_dict]
print(titles)
输出:
[u'Apple', u'Banana', u'Mango']