[英]Powershell how to parse array
I have a microsoft form that submits an array of data to a variable that I need to process.我有一个微软表单,它将一组数据提交给我需要处理的变量。 The Output array from the forms looks like this and is assigned to a variable: forms 中的 Output 数组看起来像这样并分配给一个变量:
["a","b","c"]
How can I create an array in powershell from this so I can actually call the array items like this:如何在 powershell 中创建一个数组,这样我实际上可以像这样调用数组项:
if ("a" in $array) {
# Do something
}
if ("b" in $array) {
# Do something
}
if ("c" in $array) {
# Do something
}
The output from your form, by the looks of it, is a JSON string which you can convert to an object using ConvertFrom-Json
:从您的表格中的 output 看起来,是一个 JSON 字符串,您可以使用ConvertFrom-Json
将其转换为 object :
$var = '["a","b","c"]'
$array = $var | ConvertFrom-Json
switch($array) {
a { 'a in array'; continue }
b { 'b in array'; continue }
c { 'c in array'; continue }
Default { 'nothing found' }
}
Note , the use of the continue
on above example is strictly for efficiency.注意,在上面的例子中使用continue
是为了提高效率。 From about_Switch documentation we can read the following:从about_Switch文档中,我们可以阅读以下内容:
The
Break
keyword stops processing and exits the Switch statement.Break
关键字停止处理并退出 Switch 语句。
TheContinue
keyword stops processing the current value, but continues processing any subsequent values.Continue
关键字停止处理当前值,但继续处理任何后续值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.