【问题标题】:How can I add if statements to a json dictionary in Python如何将 if 语句添加到 Python 中的 json 字典
【发布时间】:2015-03-09 11:23:04
【问题描述】:

是否可以使用 if 语句更改 json 字典负载? 我有 4 种类型的 ID 分别为 1、2、3、4,所以我需要某种选择开关代码。

原代码为:

payload = {
    "added": [
        {
            "assingedTargets": [
                {
                    "sequencing": 1,
                        "name": assigned_target_name,
                        "id": assigned_target_id
                    }
                ],
                "type": {
                    "protocol": {
                        "isDefaultProtocol": True,
                        "name": "tcp",
                        "id": 3,
                        "label": "tcp"
                    },
                    "name": target_type,
                    "id": int(1)
                },
              "name":"DDITargetGroup5"        
            }
        ]
        }

我想做的是:

payload = {
    "added": [
        {
            "assingedTargets": [
                {
                    "sequencing": 1,
                        "name": assigned_target_name,
                         "id": assigned_target_id
                    }
                ],
                "type": {
                    "protocol": {
                        "isDefaultProtocol": True,
                        "name": "tcp",
                        "id": 3,
                        "label": "tcp"
                    },
                    if target_type: == "direct dial" 
                      "name": target_type,
                      "id": int(1)
                    elif target_type == "private Wire"
                      "name": target_type
                      "id": int(2) 
                },
              "name":"DDITargetGroup5"        
            }
        ]
        }

我得到错误:

if target_type: == "direct dial"
             ^
SyntaxError: invalid syntax

Process finished with exit code 1

【问题讨论】:

  • 你想做的只是没有json。所以你不能在 json 中做到这一点。你真正想做的事情有点不清楚?
  • 对我来说这看起来像一个 python 字典,而不是 JSON 字典。 "id": int(X) 不是有效的 JSON,但它可能是有效的 python。

标签: python json payload


【解决方案1】:

如果只有两个选项,你可以使用三元运算符

{
     ...
     "name": target_type,
     "id": 1 if target_type == 'direct dial' else 2,
     ... 
}

但是,它会带来可读性的成本,因此您应该考虑将条件语句与 dict 分开是否更好。

【讨论】:

    【解决方案2】:

    您不能只将条件语句放入 json 对象中。相反,您应该创建一个变量来存储 id 值 (id_val) 并在使对象根据 target_type 的值设置其值之前使用条件。然后将字典设置为包含这些变量的值(见下文)。

    id_val = 0
    if target_type == 'direct dial':
        id_val = 1
    elif target_type == 'private Wire':
        id_val = 2
    
    payload = { "added": [ { "assingedTargets": [ { "sequencing": 1, "name": assigned_target_name, "id": assigned_target_id } ], "type": { "protocol": { "isDefaultProtocol": True, "name": "tcp", "id": 3, "label": "tcp" }, "name": target_type, "id": id_val }, "name":"DDITargetGroup5" } ] }
    

    【讨论】:

    • 或者可能只是使用:id_val = {'direct dial': 1, 'private wire': 2}.get(target_type, 0)...
    猜你喜欢
    • 2021-11-15
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多