Python:具有数组的Json

问题描述 投票:0回答:1

我正在尝试在python中构造以下JSON以发送curl PUT请求。

{"entries":[
    {"op": "create", "ip": "x.x.x.x","subnet": "y"},
    {"op": "update", "id": "<acl_entry_id>", "ip": "y.y.y.y", "subnet": "p"},
    {"op": "delete", "id": "<acl_entry_id>"}
]}

我尝试了以下逻辑:

 payload = {"op": "create", "ip": ip,"subnet": subnet}
 data = json.dumps(payload) 

这没有给我一个数组。当前数据帧如下所示:

{"subnet": "24", "ip": "x.x.x.x", "op": "create"}

我是python的新手,这里有任何建议来修复数据集吗?

python arrays json put
1个回答
0
投票

Python语法恰好与JSON语法相同。只需将变量放在需要的位置。

payload = {"entries":[
    {"op": "create", "ip": ip,"subnet": subnet},
    {"op": "update", "id": acl_entry_id, "ip": ip, "subnet": subnet},
    {"op": "delete", "id": acl_entry_id}
]}
© www.soinside.com 2019 - 2024. All rights reserved.