Powershell - 无法以哈希 table 数组的形式将数据从文件导入变量

Powershell - Not able to import data in a form of hash table array from a file into variable

我正在尝试 import/read 散列数组 table(键,值)中的文件中的数据,但无法执行,因为默认分配符号( =) 不使用,而是使用冒号 (:)。我尝试了不同的方法来读取数据,但 powershell 每次都会给我一个错误。 数据(此处截断)采用以下格式(注意等号被替换为:

[{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B"}]

如何将这些数据读入数组或变量,以便我可以使用它? 这是错误消息:

> @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B"}]
At line:1 char:7
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+       ~
Missing type name after '['.
At line:1 char:14
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+              ~~~~~~~
Unexpected token ':"Alex"' in expression or statement.
At line:1 char:21
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+                     ~
Missing argument in parameter list.
At line:1 char:40
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+                                        ~~~~~~~~
Unexpected token ':"Steve"' in expression or statement.
At line:1 char:48
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+                                                ~
Missing argument in parameter list.
At line:1 char:67
+ ... ":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"9 ...
+                                                           ~~~~~~~
Unexpected token ':"Lisa"' in expression or statement.
At line:1 char:74
+ ... :"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"90 ...
+                                                                 ~
Missing argument in parameter list.
At line:1 char:93
+ ... "Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B ...
+                                                         ~~~~~~~~~
Unexpected token ':"Jackie"' in expression or statement.
At line:1 char:102
+ ... Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B" ...
+                                                                 ~
Missing argument in parameter list.
At line:1 char:1
+ @a = [{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"L ...
+ ~~
The splatting operator '@' cannot be used to reference variables in an expression. '@a' can be used only as an
 argument to a command. To reference variables in an expression use '$a'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingTypename

您的输入似乎是 JSON,因此您需要:

  • 首先使其成为 PowerShell string[1],将其包含在 '...' (假设要使用该值 verbatim).

  • 然后使用 ConvertFrom-Json cmdlet 将字符串解析为对象图:

$arrayOfObjects = ConvertFrom-Json '[{"name":"Alex","IQ":"A+"},{"name":"Steve","IQ":"60"},{"name":"Lisa","IQ":"90"},{"name":"Jackie","IQ":"B"}]' 

$arrayOfObjects 然后包含代表 JSON 输入的 [pscustomobject] 个实例的数组;如果你输出 $arrayOfObjects,你会看到:

name   IQ
----   --
Alex   A+
Steve  60
Lisa   90
Jackie B

[1] 您问题 (=) 中作业的 RHS 以未加引号的 [ 开头,这意味着 PowerShell 在 中解析它表达式模式 ,因此期望 [ 成为 类型文字 的一部分,例如 [string] - 请参阅概念 about_Parsing帮助主题。
另外,请注意,sigil @ 永远不会在变量 assignments 中使用(其中必须使用 $),仅在 splatting 的上下文中使用;也就是说,使用 $var = ...,从不使用 @var = ...