【问题标题】:Need to convert comma separated string into json string需要将逗号分隔的字符串转换为json字符串
【发布时间】:2020-11-27 17:47:33
【问题描述】:
我将以逗号分隔的字符串 (recipient1@example.com,recipient2@example.com,recipient3@example.com ) 形式获取电子邮件 ID。
如何使用 power shell 构建这样的 json 字符串
{"personalizations": [
{"to": [
{"email": "recipient1@example.com"},
{"email": "recipient2@example.com"}
]}]}
【问题讨论】:
标签:
javascript
json
powershell
【解决方案1】:
使用String.Split() 方法拆分输入字符串,然后构造一个看起来像所需JSON 的对象,最后通过ConvertTo-Json 管道:
$recipients = "recipient1@example.com,recipient2@example.com,recipient3@example.com"
@{
personalizations = @(
@{
to = @(
$recipients.Split(',').ForEach({@{email=$_}})
)
}
)
} |ConvertTo-Json -Depth 4