Arrays 如何对PowerShell中的字符或字符串数据使用ConvertTo Json@()?

Arrays 如何对PowerShell中的字符或字符串数据使用ConvertTo Json@()?,arrays,json,powershell,type-conversion,read-eval-print-loop,Arrays,Json,Powershell,Type Conversion,Read Eval Print Loop,在往返JSON的上下文中: 也许下面的整数只是标记数组索引?但是完全可以发送1,1,1,所以它不是索引。那么,1可能表示深度 PS /home/nicholas/powershell> PS /home/nicholas/powershell> ConvertTo-Json @(1) [ 1 ] PS /home/nicholas/powershell> PS /home/nicholas/powershell> ConvertTo-Json @(1,a) P

在往返JSON的上下文中:

也许下面的整数只是标记数组索引?但是完全可以发送1,1,1,所以它不是索引。那么,1可能表示深度

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> ConvertTo-Json @(1)  
[
  1
]
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> ConvertTo-Json @(1,a)
ParserError: 
Line |
   1 |  ConvertTo-Json @(1,a)
     |                     ~
     | Missing expression after ','.

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> ConvertTo-Json @(1,2)
[
  1,
  2
]
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> ConvertTo-Json @(a)  
a: The term 'a' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
[]
PS /home/nicholas/powershell> 
为什么整数可以:

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> ConvertTo-Json @(1,3,9)
[
  1,
  3,
  9
]
PS /home/nicholas/powershell> 
但连一个字符都没有


这两种类型的字符串数据都不可接受。

PowerShell没有任何用于定义字符文本的语法,如错误所示,示例中的a等裸字被解释为命令

如果要将单个[char]a作为值传递,有许多选项:

# Convert single-char string to char
[char]'a'
# or
'a' -as [char]

# Index into string
'a'[0]

# Convert from numberic value
97 -as [char]
所以你可以这样做:

PS ~> ConvertTo-Json @(1,'a'[0])
[
    1,
    "a"
]
但正如您所注意到的,生成的JSON似乎已将[char]转换回字符串,这是因为JSON的语法也没有[char]

发件人:

JSON值必须是对象、数组、数字或字符串[…]

因此,从[string]到[char]的转换实际上是完全冗余的:

PS ~> ConvertTo-Json @(1,'a')
[
    1,
    "a"
]

PowerShell没有任何用于定义字符文本的语法,如错误所示,示例中的a等裸字被解释为命令

如果要将单个[char]a作为值传递,有许多选项:

# Convert single-char string to char
[char]'a'
# or
'a' -as [char]

# Index into string
'a'[0]

# Convert from numberic value
97 -as [char]
所以你可以这样做:

PS ~> ConvertTo-Json @(1,'a'[0])
[
    1,
    "a"
]
但正如您所注意到的,生成的JSON似乎已将[char]转换回字符串,这是因为JSON的语法也没有[char]

发件人:

JSON值必须是对象、数组、数字或字符串[…]

因此,从[string]到[char]的转换实际上是完全冗余的:

PS ~> ConvertTo-Json @(1,'a')
[
    1,
    "a"
]

JSON没有字符类型的概念,只有字符串:ConvertTo JSON@1,'a'thx,它回答了我的问题,比如它是JSON没有字符类型的概念,只有字符串:ConvertTo JSON@1,'a'thx,它回答了我的问题,比如它是