Jer*_*emy 4 python json ordereddictionary
我正在尝试创建一个嵌套的 Json 结构,如下所示:
示例 Json:
{
"id" : "de",
"Key" : "1234567",
"from" : "test@test.com",
"expires" : "2018-04-25 18:45:48.3166159",
"command" : "method.exec",
"params" : {
"method" : "cmd",
"Key" : "default",
"params" : {
"command" : "testing 23"
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试从 OrderedDict 中执行此操作。我不确定构造 OrderedDict 以便生成正确的 Json 的正确方法。
Python代码:
json_payload = OrderedDict(
[('id', id),
('Key', keystore),
('from', 'test@test.com'),
('expires', expires),
('command', 'method.exec')]
# What goes here for the params section??
)
print json.dumps(json_payload, indent=4, default=str)
Run Code Online (Sandbox Code Playgroud)
使用 @haifzhan 的输出作为输入准确地提供了所需的内容。
payload = OrderedDict(
[
('id', 'de'),
('Key', '1234567'),
('from', 'test@test.com'),
('expires', '2018-04-25 18:45:48.3166159'),
('command', 'method.exec'),
('params',
OrderedDict(
[
('method', 'cmd'),
('Key', 'default'),
('params',
OrderedDict(
[
('command', 'testing 23')
]
)
)
]
)
)
]
)
print json.dumps(json_payload, indent=4, default=str)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17633 次 |
| 最近记录: |