我正在针对日期范围内的一系列值进行API GET。
如何处理返回的值字符串并提取特定值(在这种情况下为“ day”和“ value”)
import requests
url = "https://api.something.com/rest/o/GHEYE/n/3/d/4/daily_client_count"
querystring = {
"start":"2018-11-12T02:04:05",
"end":"2018-12-13T02:04:05",
"type":"",
}
payload = ""
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'Authorization': "Bearer xxxxxx44bec4572xxxxxxxx",
'cache-control': "no-cache",
'Postman-Token': "462xxxx-2ae4-4938-bae3-7a31xxxxx"
}
response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
print(response.text)
这将返回以下字符串...
{"resp_code":"SUCCESS","caller_ref":"654656465456564","server_ref":"65464655496545561","data":[{"day":"2018-11-12","value":1125,"wifi_client_connected":0,"usages":1125},{"day":"2018-11-13","value":2292,"wifi_client_connected":0,"usages":2292},{"day":"2018-11-14","value":1772,"wifi_client_connected":0,"usages":1772},{"day":"2018-11-15","value":2068,"wifi_client_connected":0,"usages":2068},{"day":"2018-11-16","value":2309,"wifi_client_connected":0,"usages":2309},{"day":"2018-11-17","value":2479,"wifi_client_connected":0,"usages":2479},{"day":"2018-11-18","value":2341,"wifi_client_connected":0,"usages":2341},{"day":"2018-11-19","value":1550,"wifi_client_connected":0,"usages":1550},{"day":"2018-11-20","value":1417,"wifi_client_connected":0,"usages":1417},{"day":"2018-11-21","value":1734,"wifi_client_connected":0,"usages":1734},{"day":"2018-11-22","value":1954,"wifi_client_connected":0,"usages":1954},{"day":"2018-11-23","value":343,"wifi_client_connected":0,"usages":343},{"day":"2018-11-24","value":16,"wifi_client_connected":0,"usages":16},{"day":"2018-11-25","value":36,"wifi_client_connected":0,"usages":36},{"day":"2018-11-26","value":29,"wifi_client_connected":0,"usages":29},{"day":"2018-11-27","value":34,"wifi_client_connected":0,"usages":34},{"day":"2018-11-28","value":30,"wifi_client_connected":0,"usages":30},{"day":"2018-11-29","value":25,"wifi_client_connected":0,"usages":25},{"day":"2018-11-30","value":852,"wifi_client_connected":0,"usages":852},{"day":"2018-12-01","value":572,"wifi_client_connected":0,"usages":572},{"day":"2018-12-02","value":639,"wifi_client_connected":0,"usages":639},{"day":"2018-12-03","value":848,"wifi_client_connected":0,"usages":848},{"day":"2018-12-04","value":678,"wifi_client_connected":0,"usages":678},{"day":"2018-12-05","value":884,"wifi_client_connected":0,"usages":884},{"day":"2018-12-06","value":288,"wifi_client_connected":0,"usages":288},{"day":"2018-12-07","value":542,"wifi_client_connected":0,"usages":542},{"day":"2018-12-08","value":185,"wifi_client_connected":0,"usages":185},{"day":"2018-12-09"},{"day":"2018-12-10","value":353,"wifi_client_connected":0,"usages":353},{"day":"2018-12-11","value":82,"wifi_client_connected":0,"usages":82},{"day":"2018-12-12","value":18,"wifi_client_connected":0,"usages":18},{"day":"2018-12-13"}]}
[Finished in 1.264s]
我想遍历字符串,以提取每天的VALUE并暂时逐行打印它们。
谢谢。
D
答案 0 :(得分:0)
for item in response.json().get('data', []):
print('Day: {}, value: {}'.format(
item.get('day', ''),
item.get('value','')
))