无法从 JSON 个结果中检索值
can't retrieve value from JSON results
我通过 属性 搜索,将字节转换为字符串,然后从结果中获取 objectId
值,通过 REST 调用从 table 中检索条目。
new_requests = Popen(['curl',
'-H', 'application-id: %s' % backendless_appid,
'-H', 'secret-key: %s' % backendless_sk,
'-H', 'Content-Type: application/json',
'-X', 'GET',
'-v', 'https://api.backendless.com/v1/data/Request?where=requestId%3D1'],
stdout=PIPE).communicate()[0]
new_requests_str = new_requests.decode(encoding='utf-8')
new_requests_objectid = json.loads(new_requests_str, strict=False)['objectId']
不幸的是,这会导致 KeyError: 'objectId'
print(new_requests_str)
returns结果JSON,所以问题是new_requests_objectid
.
{"offset":0,"data":[{"emailAddress":"eric@apakau.com","apiEndpoint":"http://www.yahoo.com","created":1438986033000,"requestId":"1","___class":"Request","ownerId":null,"updated":1439222409000,"objectId":"723B5AEE-5D60-E00E-FF92-ACA3B4629F00","apiSecretkey":"asdfasa","__meta":"{\"relationRemovalIds\":{},\"selectedProperties\":[\"emailAddress\",\"apiEndpoint\",\"created\",\"requestId\",\"___class\",\"ownerId\",\"updated\",\"objectId\",\"apiSecretkey\"],\"relatedObjects\":{}}"}],"nextPage":null,"totalObjects":1}
objectId
不是 new_requests json 中的直接键。相反,它是 data
列表中字典的一部分。所以你需要像那样访问它。很可能请求 return 有多个对象(和多个 objectId
s)的响应,所以我建议将它们保存在一个列表中,示例 -
new_requests = Popen(['curl',
'-H', 'application-id: %s' % backendless_appid,
'-H', 'secret-key: %s' % backendless_sk,
'-H', 'Content-Type: application/json',
'-X', 'GET',
'-v', 'https://api.backendless.com/v1/data/Request?where=requestId%3D1'],
stdout=PIPE).communicate()[0]
new_requests_str = new_requests.decode(encoding='utf-8')
new_requests_objectIds = []
for data_item in json.loads(new_requests_str, strict=False)['data']:
new_requests_objectIds.append(data_item['objectId'])
我通过 属性 搜索,将字节转换为字符串,然后从结果中获取 objectId
值,通过 REST 调用从 table 中检索条目。
new_requests = Popen(['curl',
'-H', 'application-id: %s' % backendless_appid,
'-H', 'secret-key: %s' % backendless_sk,
'-H', 'Content-Type: application/json',
'-X', 'GET',
'-v', 'https://api.backendless.com/v1/data/Request?where=requestId%3D1'],
stdout=PIPE).communicate()[0]
new_requests_str = new_requests.decode(encoding='utf-8')
new_requests_objectid = json.loads(new_requests_str, strict=False)['objectId']
不幸的是,这会导致 KeyError: 'objectId'
print(new_requests_str)
returns结果JSON,所以问题是new_requests_objectid
.
{"offset":0,"data":[{"emailAddress":"eric@apakau.com","apiEndpoint":"http://www.yahoo.com","created":1438986033000,"requestId":"1","___class":"Request","ownerId":null,"updated":1439222409000,"objectId":"723B5AEE-5D60-E00E-FF92-ACA3B4629F00","apiSecretkey":"asdfasa","__meta":"{\"relationRemovalIds\":{},\"selectedProperties\":[\"emailAddress\",\"apiEndpoint\",\"created\",\"requestId\",\"___class\",\"ownerId\",\"updated\",\"objectId\",\"apiSecretkey\"],\"relatedObjects\":{}}"}],"nextPage":null,"totalObjects":1}
objectId
不是 new_requests json 中的直接键。相反,它是 data
列表中字典的一部分。所以你需要像那样访问它。很可能请求 return 有多个对象(和多个 objectId
s)的响应,所以我建议将它们保存在一个列表中,示例 -
new_requests = Popen(['curl',
'-H', 'application-id: %s' % backendless_appid,
'-H', 'secret-key: %s' % backendless_sk,
'-H', 'Content-Type: application/json',
'-X', 'GET',
'-v', 'https://api.backendless.com/v1/data/Request?where=requestId%3D1'],
stdout=PIPE).communicate()[0]
new_requests_str = new_requests.decode(encoding='utf-8')
new_requests_objectIds = []
for data_item in json.loads(new_requests_str, strict=False)['data']:
new_requests_objectIds.append(data_item['objectId'])