批处理之家's Archiver

5i365 发表于 2022-5-15 06:58

【已解决】PowerShell用hash表+函数的方式转换为json字符串

[i=s] 本帖最后由 5i365 于 2022-5-15 09:06 编辑 [/i]

[size=4]下面的代码, 是用一行字符串来表达的json的, 不太好观察结构![/size][size=4]
[/size]
[size=4]我想把它改成 hash表+函数的方式, 在frames[]处卡壳了, 求高手指引, 提前感谢!
原代码:[code]$BuildName = 'he'
$Message = 'bei'
$Payload = '{"priority":"critical","icon_type":"alert","lifeTime":2000,"model":{"frames":[{"icon":"555","text":"' + $BuildName + ' BUILD FAILED! ' + $Message + '"}],"sound":{"category":"alarms","id":"alarm6","repeat":2},"cycles":0}}'
$Payload[/code]我的代码:[code]$Payload = @{
        "priority"  = "critical"
        "icon_type" = "alert"
        "lifeTime"  = 2000
        "model" = @{
                "frames" = "卡这里了"
                "sound"  = @{
                        "category" = "alarms"
                        "id"     = "alarm6"
                        "repeat"   = 2
                }
                "cycles" = 0
        }
} | ConvertTo-Json
$Payload
[/code][/size]

flashercs 发表于 2022-5-15 08:19

[code]$Payload = @{
        "priority"  = "critical"
        "icon_type" = "alert"
        "lifeTime"  = 2000
        "model" = @{
                "frames" = @(
                @{
                icon="555"
                text=$BuildName + ' BUILD FAILED! ' + $Message
                }
                )
                "sound"  = @{
                        "category" = "alarms"
                        "id"     = "alarm6"
                        "repeat"   = 2
                }
                "cycles" = 0
        }
} | ConvertTo-Json -Depth 10[/code]

5i365 发表于 2022-5-15 09:04

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=255701&ptid=62735]2#[/url] [i]flashercs[/i] [/b]


   感谢大侠指引, 原来我卡在这个参数了, [color=#c0be][font=Monaco, Consolas, &quot][size=12px] -Depth 10[/size][/font][/color]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.