python - 无法将 json 转储变量转换到单独的行
问题描述
我正在尝试将 json 转储打印到多行。目前它在一行上打印。
这是我正在尝试的步骤;
第 1 步:这是我要打印的代码
payload = json.dumps({"ip-detunnel": "No",
"name": "DP800 Slot 1",
"vlan-tag-insertion": "No",
"vlan-tag-remove-forward": "Remove",
"mac-replace-header": "No",
"mpls-label-stack": "Pass Through"
})
第2步:这就是我尝试打印的方式
print '\n' + payload
第 3 步:结果如下:
{"vlan-tag-insertion": "No", "name": "DP800 Slot 1", "mpls-label-stack": "Pass Through", "vlan-tag-remove-forward": "Remove", "ip-detunnel": "No", "mac-replace-header": "No"}
我的目标是在多行上打印结果,使其看起来像步骤 1 中的 json.dumps
解决方案
传给indent=4
_dumps
payload = json.dumps({"ip-detunnel": "No", "name": "DP800 Slot 1", "vlan-tag-insertion": "No", "vlan-tag-remove-forward": "Remove", "mac-replace-header": "No", "mpls-label-stack": "Pass Through" }, indent=4)
或除 0 以外的任何其他数字。 json 对象的每个嵌套都将缩进它之前的那么多空格。输出:
>>>print(payload)
{
"ip-detunnel": "No",
"name": "DP800 Slot 1",
"vlan-tag-insertion": "No",
"vlan-tag-remove-forward": "Remove",
"mac-replace-header": "No",
"mpls-label-stack": "Pass Through"
}
推荐阅读
- javascript - 包含两个方括号的简单 JavaScript 正则表达式
- python - 如何通过包含的内容删除文本文件
- entity-framework-core - 使用部分硬编码的复合外键将实体映射到查找表
- python - 将多个字典/列表列展平为新的列和行
- r - Rstudio typeError未定义
- tensorflow - Keras 损失总是 0.0000e+00
- authorize.net - 通过新的 Authorize.Net API 与 AIM API 进行退款
- wordpress - 带有复选框 Woocommerce 的条件必填字段
- asp.net-core - 定义多对多关系
- oauth-2.0 - 在 Postman 中授权 AzureAD 时如何获取 v2 jwt 令牌