我有一个变量“ case”,它调用OrderedDict:
OrderedDict([('totalSize', 1),
('done', True),
('records',
[OrderedDict([('attributes',
OrderedDict([('type', 'Case'),
('url',
'/services/data/v38.0/sobjects/Case/5003700000ReKcJAAV')])),
('Id', '5003700000ReKcJAAV')])])])
我希望变量“ Id”返回“ 5003700000ReKcJAAV”。有办法吗?
我正在使用Python 3.6.5
答案 0 :(得分:1)
这看起来很丑,但是行得通。
from collections import OrderedDict
z = OrderedDict([('totalSize', 1),
('done', True),
('records',
[OrderedDict([('attributes',
OrderedDict([('type', 'Case'),
('url',
'/services/data/v38.0/sobjects/Case/5003700000ReKcJAAV')])),
('Id', '5003700000ReKcJAAV')])])])
print(z.get('records')[0].get('Id'))
输出:
5003700000ReKcJAAV
答案 1 :(得分:0)
您也可以尝试以下代码。
from collections import OrderedDict
import json
l = [('totalSize', 1),
('done', True),
('records',
[OrderedDict([('attributes',
OrderedDict([('type', 'Case'),
('url',
'/services/data/v38.0/sobjects/Case/5003700000ReKcJAAV')])),
('Id', '5003700000ReKcJAAV')])])]
ordDict = OrderedDict(l)
print json.dumps(ordDict, indent=4)
"""
{
"totalSize": 1,
"done": true,
"records": [
{
"attributes": {
"type": "Case",
"url": "/services/data/v38.0/sobjects/Case/5003700000ReKcJAAV"
},
"Id": "5003700000ReKcJAAV"
}
]
}
"""
print ordDict['records'][0]['Id'] # 5003700000ReKcJAAV
答案 2 :(得分:0)
这很容易,然后我就期待了。我只需要:
Id = case['records'][0]['Id']