具有数组的复杂哈希表,可在PowerShell中转换为JSON格式

时间:2018-11-09 16:40:21

标签: json powershell

我正在尝试向Microsoft Teams发送MessageCard,因此我需要从许多变量中构造JSON。我的问题是哈希表中的嵌套数组以对象的type而不是value出现:

PowerShell:

$jsonHash = [ordered]@{
    '@type' = "MessageCard"
    '@context' = "https://schema.org/extensions"
    'summary' = $SummaryText
    'themeColor' = $ThemeColor
    'title' = $Title                
    'sections' = @(
        [ordered]@{
            'activityTitle' = $ActivityTitle
            'activitySubTitle' = $ActivitySubTitle
            'activityImage' = $ActivityImageUrl
            'text' = $BodyText
            'facts' = @(
                [ordered]@{
                    'name' = 'Computer'
                    'value' = $env:COMPUTERNAME
                },
                [ordered]@{
                    'name' = 'IP Address'
                    'value' = $((Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected"}).IPv4Address.IPAddress)
                }
            )
        }                                        
    )
    'potentialAction' = @(
        [ordered]@{
            '@type' = 'HttpPOST'
            'name' = 'Activate'
            'target' = $targetUrl
        }
    )                
}
return $jsonHash | ConvertTo-Json

此输出显示facts标签为System.Collections.Specialized.OrderedDirectionary,而不是我期望的正确JSON输出:

JSON输出:

{
"@type":  "MessageCard",
"@context":  "https://schema.org/extensions",
"summary":  "Node Activation for TESTPC",
"themeColor":  "0078D7",
"title":  null,
"sections":  [
                 {
                     "activityTitle":  "Network Node TESTPC requesting activation",
                     "activitySubTitle":  null,
                     "activityImage":  "<removed>",
                     "text":  "Some text here",
                     "facts":  "System.Collections.Specialized.OrderedDictionary System.Collections.Specialized.OrderedDictionary"
                 }
             ],
"potentialAction":  [
                        {
                            "@type":  "HttpPOST",
                            "name":  "Activate",
                            "header":  "System.Collections.Hashtable",
                            "target":  "<removed>"
                        }
                    ]
}

即使header的值也显示为System.Collections.Hashtable,而不是正确的值。

关于如何使用此复杂对象正确格式化JSON的任何想法?

0 个答案:

没有答案