访问 python 中的 json 字段
Access a json field in python
我在 python
中使用获取请求
x = requests.get(url, headers = ... )
print(x.json())
我得到的结果是
{
'organization': {
'id': '6',
'name': 'TestPostman',
'displayName': 'TestPostman',
'canHaveGateways': True,
'maxGatewayCount': 0,
'maxDeviceCount': 0
},
'createdAt': '2022-05-10T18:07:49.327175Z',
'updatedAt': '2022-05-10T19:44:09.667978Z'
}
我如何访问组织的任何字段?
我尝试了 x.organizzation['id'] 但没有用。
如果您想访问组织中的 ID,试试这个:
x = x.json()
print(x["organization"]["id"])
类似的方法,但这次有点不同。我得到的输出是
{
"totalCount": "1",
"result": [
{
"id": "62",
"name": "HOME",
"displayName": "HOME",
"canHaveGateways": true,
"createdAt": "2022-05-12T13:50:10.303205Z",
"updatedAt": "2022-05-12T13:50:10.303205Z"
}
]
}
我尝试了 print(x["result"]["id"])
但由于结果是这样 result[{}]
它不起作用。对此有什么想法吗?
我在 python
中使用获取请求x = requests.get(url, headers = ... )
print(x.json())
我得到的结果是
{
'organization': {
'id': '6',
'name': 'TestPostman',
'displayName': 'TestPostman',
'canHaveGateways': True,
'maxGatewayCount': 0,
'maxDeviceCount': 0
},
'createdAt': '2022-05-10T18:07:49.327175Z',
'updatedAt': '2022-05-10T19:44:09.667978Z'
}
我如何访问组织的任何字段? 我尝试了 x.organizzation['id'] 但没有用。
如果您想访问组织中的 ID,试试这个:
x = x.json()
print(x["organization"]["id"])
类似的方法,但这次有点不同。我得到的输出是
{
"totalCount": "1",
"result": [
{
"id": "62",
"name": "HOME",
"displayName": "HOME",
"canHaveGateways": true,
"createdAt": "2022-05-12T13:50:10.303205Z",
"updatedAt": "2022-05-12T13:50:10.303205Z"
}
]
}
我尝试了 print(x["result"]["id"])
但由于结果是这样 result[{}]
它不起作用。对此有什么想法吗?