作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一段代码(向 Microsoft Teams 发送消息的脚本的一部分)将一些 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")
}
)
}
)
}
生成此代码时,“事实”部分中“事实”的数量是未知的。我想使用变量将 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")
}
)
}
)
但是结果失败了,似乎无论我做什么,都会向变量中添加额外的字符:
{
"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": " "
}
关于json - Powershell Convert-To Json - 动态添加内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47444156/
我是一名优秀的程序员,十分优秀!