Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell转换为JSON-动态添加内容_Json_Powershell_Variables - Fatal编程技术网

Powershell转换为JSON-动态添加内容

Powershell转换为JSON-动态添加内容,json,powershell,variables,Json,Powershell,Variables,我有一段代码(向Microsoft团队发送消息的脚本的一部分),它将一些JSON存储在一个变量中: $body = ConvertTo-Json @{ title = "$($messageTitle)" text = " " sections = @( @{ activityTitle = "$($activityTitle)" activitySubtitle = " "

我有一段代码(向Microsoft团队发送消息的脚本的一部分),它将一些JSON存储在一个变量中:

$body = ConvertTo-Json @{
    title    = "$($messageTitle)"
    text     = " "
    sections = @(
        @{
            activityTitle    = "$($activityTitle)"
            activitySubtitle = " "
            activityImage    = "$imageLink"
        },
        @{
            title          = 'Details'
            facts          = @(
                @{
                    name  = 'Name1'
                    value = ''
                })
            potentialAction = @(@{
                    '@context' = 'http://schema.org'
                    '@type'    = 'ViewAction'
                    name       = 'Button Name'
                    target   = @("https://google.com.au")
                }                   
            )
        }
    )

}
{
"title":  "Test Title",
"sections":  [
                 {
                     "activitySubtitle":  " ",
                     "activityImage":  "http://icons.iconarchive.com/icons/double-j-design/origami-colored-pencil/128/green-ok-icon.png",
                     "activityTitle":  "test Activity"
                 },
                 {
                     "facts":  "[\r\n    {\r\n        \"value\":  \"value1\",\r\n        \"name\":  \"name1\"\r\n    },\r\n    {\r\n        \"value\":  \"value2\",\r\n        \"name\":  \"name2\"\r\n    },\r\n    {\r\n        \"value\":  \"value3\",\r\n        \"name\":  \"name3\"\r\n    }\r\n]",
                     "title":  "Details",
                     "potentialAction":  "System.Collections.Hashtable"
                 }
             ],
"text":  " "
生成此代码时,“事实”部分中的“事实”数量未知。我想使用一个变量将X个名称/值对动态添加到此区域,例如:

facts          = @(
            $facts                  
            })
在该变量中,有多个名称/值对:

@{name='name1',value='value1'},@{name='name2',value='value2'},@{name='name3',value='value3'}
然而,我正在与格式化作斗争,到目前为止我还没有尝试过任何东西,引号等的组合都不起作用,结果总是无效的JSON代码

有谁能告诉我将这些值插入代码的最佳方法吗

我最近的迭代尝试在插入变量$facts之前将其转换为JSON:

$facts = convertto-json @(
    @{
        name  = 'name1'
        value = 'value1'
    },
    @{
        name  = 'name2'
        value = 'value2'
    },
    @{
        name  = 'name3'
        value = 'value3'
    }
)

$body = ConvertTo-Json @{
    title    = "$($messageTitle)"
    text     = " "
    sections = @(
        @{
            activityTitle    = "$($activityTitle)"
            activitySubtitle = " "
            activityImage    = "$imageLink"
        },
        @{
            title          = 'Details'
            facts          = @(
                    $facts
                )
            potentialAction = @(@{
                    '@context' = 'http://schema.org'
                    '@type'    = 'ViewAction'
                    name       = 'Button Name'
                    target   = @("https://google.com.au")
                }                   
            )
        }
    )
但是结果失败了,似乎无论我做什么,都会在变量中添加额外的字符:

$body = ConvertTo-Json @{
    title    = "$($messageTitle)"
    text     = " "
    sections = @(
        @{
            activityTitle    = "$($activityTitle)"
            activitySubtitle = " "
            activityImage    = "$imageLink"
        },
        @{
            title          = 'Details'
            facts          = @(
                @{
                    name  = 'Name1'
                    value = ''
                })
            potentialAction = @(@{
                    '@context' = 'http://schema.org'
                    '@type'    = 'ViewAction'
                    name       = 'Button Name'
                    target   = @("https://google.com.au")
                }                   
            )
        }
    )

}
{
"title":  "Test Title",
"sections":  [
                 {
                     "activitySubtitle":  " ",
                     "activityImage":  "http://icons.iconarchive.com/icons/double-j-design/origami-colored-pencil/128/green-ok-icon.png",
                     "activityTitle":  "test Activity"
                 },
                 {
                     "facts":  "[\r\n    {\r\n        \"value\":  \"value1\",\r\n        \"name\":  \"name1\"\r\n    },\r\n    {\r\n        \"value\":  \"value2\",\r\n        \"name\":  \"name2\"\r\n    },\r\n    {\r\n        \"value\":  \"value3\",\r\n        \"name\":  \"name3\"\r\n    }\r\n]",
                     "title":  "Details",
                     "potentialAction":  "System.Collections.Hashtable"
                 }
             ],
"text":  " "
}

非常感谢您的帮助

编辑:

作为第二个示例(第一个非常糟糕),它试图将其用作普通字符串:

$facts = '@(
    @{
        name  = "name1"
        value = "value1"
    },
    @{
        name  = "name2"
        value = "value2"
    },
    @{
        name  = "name3"
        value = "value3"
    }
)'

$body = ConvertTo-Json @{
    title    = "$($messageTitle)"
    text     = " "
    sections = @(
        @{
            activityTitle    = "$($activityTitle)"
            activitySubtitle = " "
            activityImage    = "$imageLink"
        },
        @{
            title          = 'Details'
            facts          = $facts
            potentialAction = @(@{
                    '@context' = 'http://schema.org'
                    '@type'    = 'ViewAction'
                    name       = 'Button Name'
                    target   = @("https://google.com.au")
                }                   
            )
        }
    )
其输出是相同的、错误的JSON负载

实际的JSON输出的格式非常简单:

"facts":  "@(\r\n\t\t@{\r\n\t\t\tname  = \"name1\"\r\n\t\t\tvalue = \"value1\"\r\n\t\t},\r\n\t\t@{\r\n\t\t\tname  = \"name2\"\r\n\t\t\tvalue = \"value2\"\r\n\t\t},\r\n\t\t@{\r\n\t\t\tname  = \"name3\"\r\n\t\t\tvalue = \"value3\"\r\n\t\t}\r\n\t)",
很明显,我只是没有使用正确的格式化技术组合

“事实”部分应该是什么样子的示例:

facts = @(
            @{
            name = 'Current State'
            value = $($status)
            },
            @{
            name = 'Message'
            value = $($message)
            },
            @{
            name = 'Since'
            value =$($since)
            },
            @{
            name = 'Last up'
            value = $($lastup)
            },
            @{
            name = 'Sensor'
            value = $($sensorURL)
            },
            @{
            name = 'Device'
            value = $($deviceURL)
            },
            @{
            name = 'Management URL'
            value = $($serviceURL)
            }
        )

不要将$facts转换为JSON,将其保留为哈希表数组

$facts = @(
    @{
        name  = 'name1'
        value = 'value1'
    },
    @{
        name  = 'name2'
        value = 'value2'
    },
    @{
        name  = 'name3'
        value = 'value3'
    }
)
然后确保为转换设置深度

$body = ConvertTo-Json @{
    title    = "$($messageTitle)"
    text     = " "
    sections = @(
        @{
            activityTitle    = "$($activityTitle)"
            activitySubtitle = " "
            activityImage    = "$imageLink"
        },
        @{
            title          = 'Details'
            facts          = $facts
            potentialAction = @(@{
                    '@context' = 'http://schema.org'
                    '@type'    = 'ViewAction'
                    name       = 'Button Name'
                    target   = @("https://google.com.au")
                }                   
            )
        }
    )
} -Depth 5
我武断地挑了5个,对这样一个小任务估计过高是没有坏处的,我确信这已经足够了

$body > test.json
给了我们这样的东西

{
    "title":  "",
    "sections":  [
                     {
                         "activitySubtitle":  " ",
                         "activityImage":  "",
                         "activityTitle":  ""
                     },
                     {
                         "facts":  [
                                       {
                                           "value":  "value1",
                                           "name":  "name1"
                                       },
                                       {
                                           "value":  "value2",
                                           "name":  "name2"
                                       },
                                       {
                                           "value":  "value3",
                                           "name":  "name3"
                                       }
                                   ],
                         "title":  "Details",
                         "potentialAction":  [
                                                 {
                                                     "@context":  "http://schema.org",
                                                     "name":  "Button Name",
                                                     "target":  [
                                                                    "https://google.com.au"
                                                                ],
                                                     "@type":  "ViewAction"
                                                 }
                                             ]
                     }
                 ],
    "text":  " "
}

你似乎在双重转换。在第一节中,您将
$facts
转换为json,然后在第二节中,您将已经存在的json字符串添加到一个对象中,然后再转换为json。@eris我刚刚将其用作最新的示例,我现在已经尝试了很多方法,例如字符串、哈希表、已经“json”化的字符串。到目前为止,它们都不起作用。@Eris我在OP中添加了另一个我尝试过的示例。在输入JSON字符串时,它显然是一个正确格式化字符串的问题。我尝试了所有这些和更多的组合,我相信我缺少的是-depth参数!我今天早上刚刚测试过,它似乎是正确的!如果没有-Depth参数,“facts”部分看起来像这样——“facts”:“System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable”,我错误地将其视为一个错误,没有意识到它只是让它崩溃了。这个问题早就解决了!非常感谢您花时间如此彻底地回答并帮助我解决问题。