ActiveCollab REST API 使用 Python [已解决] 收到的嵌套 JSON 中的访问值
Access value in nested JSON recieved by ActiveCollab REST API using Python [Solved]
我通过 REST-API 从 ActiveCollab 得到这个:
{
"time_records": [
{
"id": 122,
"class": "TimeRecord",
"url_path": "\/projects\/89\/time-records\/122",
"is_trashed": false,
"trashed_on": null,
"trashed_by_id": 0,
"billable_status": 1,
"value": 1.0,
"record_date": 1645847200,
"summary": "example",
"user_id": 12365,
"user_name": "Fred Coal",
"user_email": "fredc@example.com",
"parent_type": "Project",
"parent_id": 89,
"created_on": 1628497959,
"created_by_id": 17,
"created_by_name": "Fred Coal",
"created_by_email": "fredc@example.com",
"updated_on": 1635784512,
"updated_by_id": 17,
"job_type_id": 1,
"source": "unknown",
"original_is_trashed": false
},
],
}
我想获取“值”的值。
但我不知道如何访问此 属性。
我正在为此使用 Python。
import requests
import json
startdate = "2021-08-01"
enddate = "2021-08-31"
fromto = "?from="+startdate+"&to="+enddate
#--------------
def ac_rq(endpoint):
token ='*'
url = "https://*/api/v1/"+endpoint
resp = dict(requests.get(url, headers={'X-Angie-AuthApiToken': token}))
print(resp)
return resp
#--------------
a = ac_rq('projects/129/time-records/filtered-by-date'+str(fromto))
b = json.loads(a.text)
for i in b:
print(i['time_records'][0]['value'])
input("press enter to exit")
我收到这个错误:
类型错误:字符串索引必须是整数
正如@rdas 在评论中提到的,您可以通过以下方式访问“值”:response['time_records'][0]['value']
见下文(将 json 字符串加载到字典中并引用请求的条目)
import json
data = '''{
"time_records": [
{
"id": 122,
"class": "TimeRecord",
"url_path": "\/projects\/89\/time-records\/122",
"is_trashed": false,
"trashed_on": null,
"trashed_by_id": 0,
"billable_status": 1,
"value": 1.0,
"record_date": 1645847200,
"summary": "example",
"user_id": 12365,
"user_name": "Fred Coal",
"user_email": "fredc@example.com",
"parent_type": "Project",
"parent_id": 89,
"created_on": 1628497959,
"created_by_id": 17,
"created_by_name": "Fred Coal",
"created_by_email": "fredc@example.com",
"updated_on": 1635784512,
"updated_by_id": 17,
"job_type_id": 1,
"source": "unknown",
"original_is_trashed": false
}
]
}'''
data = json.loads(data)
print(data['time_records'][0]['value'])
输出
1.0
我通过 REST-API 从 ActiveCollab 得到这个:
{
"time_records": [
{
"id": 122,
"class": "TimeRecord",
"url_path": "\/projects\/89\/time-records\/122",
"is_trashed": false,
"trashed_on": null,
"trashed_by_id": 0,
"billable_status": 1,
"value": 1.0,
"record_date": 1645847200,
"summary": "example",
"user_id": 12365,
"user_name": "Fred Coal",
"user_email": "fredc@example.com",
"parent_type": "Project",
"parent_id": 89,
"created_on": 1628497959,
"created_by_id": 17,
"created_by_name": "Fred Coal",
"created_by_email": "fredc@example.com",
"updated_on": 1635784512,
"updated_by_id": 17,
"job_type_id": 1,
"source": "unknown",
"original_is_trashed": false
},
],
}
我想获取“值”的值。
但我不知道如何访问此 属性。
我正在为此使用 Python。
import requests
import json
startdate = "2021-08-01"
enddate = "2021-08-31"
fromto = "?from="+startdate+"&to="+enddate
#--------------
def ac_rq(endpoint):
token ='*'
url = "https://*/api/v1/"+endpoint
resp = dict(requests.get(url, headers={'X-Angie-AuthApiToken': token}))
print(resp)
return resp
#--------------
a = ac_rq('projects/129/time-records/filtered-by-date'+str(fromto))
b = json.loads(a.text)
for i in b:
print(i['time_records'][0]['value'])
input("press enter to exit")
我收到这个错误:
类型错误:字符串索引必须是整数
正如@rdas 在评论中提到的,您可以通过以下方式访问“值”:response['time_records'][0]['value']
见下文(将 json 字符串加载到字典中并引用请求的条目)
import json
data = '''{
"time_records": [
{
"id": 122,
"class": "TimeRecord",
"url_path": "\/projects\/89\/time-records\/122",
"is_trashed": false,
"trashed_on": null,
"trashed_by_id": 0,
"billable_status": 1,
"value": 1.0,
"record_date": 1645847200,
"summary": "example",
"user_id": 12365,
"user_name": "Fred Coal",
"user_email": "fredc@example.com",
"parent_type": "Project",
"parent_id": 89,
"created_on": 1628497959,
"created_by_id": 17,
"created_by_name": "Fred Coal",
"created_by_email": "fredc@example.com",
"updated_on": 1635784512,
"updated_by_id": 17,
"job_type_id": 1,
"source": "unknown",
"original_is_trashed": false
}
]
}'''
data = json.loads(data)
print(data['time_records'][0]['value'])
输出
1.0