use*_*381 -1 python dictionary python-3.x
我有一个str像这样的有效载荷
payload = "{\"fqdn\":\"examplazdazdazazdzadza.com\",\"duration\":5,\"owner\":{\"city\":\"Paris\",\"given\":\"Alice\",\"family\":\"Doe\",\"zip\":\"75001\",\"country\":\"FR\",\"streetaddr\":\"5 rue neuve\",\"phone\":\"+33.123456789\",\"type\":0,\"email\":\"alice@example.org\"}}"
Run Code Online (Sandbox Code Playgroud)
这是来自 Gandi API的负载
我想让payload更动态一点,有一定的灵活性,所以我累了 dict
domain = 'example.com`
payload = {
'fqdn': domain,
'duration': 1,
'owner': {
"city": "Paris",
"given": "Alice",
"family": "Doe",
"zip": "75001",
"country": "FR",
"streetaddr": "5 rue neuve",
"phone": "+33.123456789",
"state": "FR-J",
"type": 0,
"email": "alice@example.org"
}
}
Run Code Online (Sandbox Code Playgroud)
在此之后,我需要恢复到原始日期类型(str),我这样做
payload = '\n'.join('\%s\: "\%s\"' % (k, v) for k, v in payload.items())
然而这会返回
错误的请求
.
任何想法如何正确完成这项工作?
可以使用JSON Module:
In [409]: import json
In [410]: json.dumps(payload)
Out[410]: '{"fqdn": "domain", "duration": 1, "owner": {"city": "Paris", "given": "Alice", "family": "Doe", "zip": "75001", "country": "FR", "streetaddr": "5 rue neuve", "phone": "+33.123456789", "state": "FR-J", "type": 0, "email": "alice@example.org"}}'
Run Code Online (Sandbox Code Playgroud)
In [411]: domain = 'example.com'
In [412]: payload = {
...: 'fqdn': domain,
...: 'duration': 1,
...: 'owner': {
...: "city": "Paris",
...: "given": "Alice",
...: "family": "Doe",
...: "zip": "75001",
...: "country": "FR",
...: "streetaddr": "5 rue neuve",
...: "phone": "+33.123456789",
...: "state": "FR-J",
...: "type": 0,
...: "email": "alice@example.org"
...: }
...: }
In [413]: json.dumps(payload)
Out[413]: '{"fqdn": "example.com", "duration": 1, "owner": {"city": "Paris", "given": "Alice", "family": "Doe", "zip": "75001", "country": "FR", "streetaddr": "5 rue neuve", "phone": "+33.123456789", "state": "FR-J", "type": 0, "email": "alice@example.org"}}'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
184 次 |
| 最近记录: |