【问题标题】:How do I pass variables through a Json body Powershell如何通过 Json 主体 Powershell 传递变量
【发布时间】:2021-05-13 05:48:45
【问题描述】:

我想为这些值传递变量,但我不能让它们去,例如在 user_id 我想传递变量 $userID 这是我正在使用的 Body 的一个示例:

$body = '{

    "data":
    [
    {
     "user_id":$userID,
     "type":"manual",
     "date":"2021-01-30",
     "duration":"150",
     "jobcode_id":"15281216",
     "notes":"This is a test of a manual time entry",
     "customfields": {
      "54138" : "IT Services",
      "54136" : "Yes"
      }
     }
     ]
     }'

【问题讨论】:

    标签: json string powershell variables scripting


    【解决方案1】:

    在您的示例中 $userID 未被替换为它的值的原因是因为您使用的是单引号 (') 字符。 PowerShell 仅在您使用双引号 (") 字符时进行替换。

    这会给您带来挑战,即您的数据已经包含双引号。 Theo's answers 中的字符串在这里工作得很好,但作为个人喜好,我会使用 PowerShell 哈希表来构造一个对象并使用 Convert-To-Json 将其转换为 Json。

    例子:

    $userID = 'John'
    $body = @{
        "data" = ,@{
            "user_id" = $userID;
            "type" = "manual";
            "date" = "2021-01-30";
            "duration" = "150";
            "jobcode_id" = "15281216";
            "notes" = "This is a test of a manual time entry";
            "customfield" = @{
                "54138" = "IT Services";
                "54136" = "Yes";
            }   
        }
    }
    
    $body | ConvertTo-Json -Depth 3
    

    输出:

    {
        "data":  [
                     {
                         "notes":  "This is a test of a manual time entry",
                         "customfield":  {
                                             "54138":  "IT Services",
                                             "54136":  "Yes"
                                         },
                         "duration":  "150",
                         "type":  "manual",
                         "date":  "2021-01-30",
                         "jobcode_id":  "15281216",
                         "user_id":  "John"
                     }
                 ]
    }
    

    编辑:正如 robdy 在 cmets 中提到的,应该使用 Depth 参数(我已经添加了它)。一个很好的解释可以找到here

    【讨论】:

    • 我还提到要添加-Depth,以便正确转换customfield下的哈希表。
    【解决方案2】:

    我会为此使用双引号的 Here-String:

    $userID = 'Alex'
    $body = @"
    {
        "data": [{
            "user_id": "$userID",
            "type": "manual",
            "date": "2021-01-30",
            "duration": "150",
            "jobcode_id": "15281216",
            "notes": "This is a test of a manual time entry",
            "customfields": {
                "54138": "IT Services",
                "54136": "Yes"
            }
        }]
    }
    "@
    

    $body 现在包含:

    {
        "data": [{
            "user_id": "Alex",
            "type": "manual",
            "date": "2021-01-30",
            "duration": "150",
            "jobcode_id": "15281216",
            "notes": "This is a test of a manual time entry",
            "customfields": {
                "54138": "IT Services",
                "54136": "Yes"
            }
        }]
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      相关资源
      最近更新 更多