Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell“$g=$json | ConvertFrom json | Group State";结果不同于$g=$json |从json转换而来$g=$g |组状态&引用;_Powershell - Fatal编程技术网

PowerShell“$g=$json | ConvertFrom json | Group State";结果不同于$g=$json |从json转换而来$g=$g |组状态&引用;

PowerShell“$g=$json | ConvertFrom json | Group State";结果不同于$g=$json |从json转换而来$g=$g |组状态&引用;,powershell,Powershell,下面的测试2和测试3的结果不同。 我对它感到困惑,因为它看起来像相同的逻辑,不同于LinuxBash|逻辑 $data=@( [PSCustomObject]@{State=“TAIPEI”;Type=“1”} [PSCustomObject]@{State=“TAIPEI”;Type=“2”} [PSCustomObject]@{State=“KH”;Type=“3”} [PSCustomObject]@{State=“KH”;Type=“4”} [PSCustomObject]@{State

下面的测试2和测试3的结果不同。
我对它感到困惑,因为它看起来像相同的逻辑,不同于LinuxBash
|
逻辑

$data=@(
[PSCustomObject]@{State=“TAIPEI”;Type=“1”}
[PSCustomObject]@{State=“TAIPEI”;Type=“2”}
[PSCustomObject]@{State=“KH”;Type=“3”}
[PSCustomObject]@{State=“KH”;Type=“4”}
[PSCustomObject]@{State=“KH”;Type=“5”}
[PSCustomObject]@{State=“TX”;Type=“6”}
[PSCustomObject]@{State=“TX”;Type=“7”}
)
$json=$data |转换为json;
#测试1
$g=$data |组状态;
写入主机($g.Length)#结果:3
#测试2
$g=$json |从json |组状态转换而来;
写入主机($g.Length)#结果:1
#测试3
$g=$json |从json转换而来;
$g=$g |组状态;
写入主机($g.Length)#结果:3

我的尝试:
我使用vscode调试模式检查变量

test1是[Object[3]]和3 GroupInfo

test2它将成为一个集合

test3是[Object[3]]和3 GroupInfo

我第一次在Powershell 7中尝试了您的示例,但无法再现您的结果,一切都按预期进行。因此,这似乎是仅限于Powershell 5的行为

查看两个版本中从Json的
ConvertFrom获得的输出,您可以看到发生了什么:

在Powershell 7中:

   # ~>  $json | ConvertFrom-Json | get-member

   TypeName: System.Management.Automation.PSCustomObject
在Powershell 5.1中:

# ~> $json | ConvertFrom-Json | Get-Member

   TypeName: System.Object[]
因此,在Powershell 5.1
convertfromJSON
中,似乎没有输出
PSCustomObject
,而
Group Object
需要正常运行。我找到了一个简单的解决方法,可以将输出从
convertfromjson
转换为
PSCustomObject
works:

# ~> $g = $json | ConvertFrom-Json | %{[PsCustomObject]$_} | group state
# ~> $g

Count Name                      Group
----- ----                      -----
    2 TAIPEI                    {@{State=TAIPEI; Type=1}, @{State=TAIPEI; Type=2}}
    3 KH                        {@{State=KH; Type=3}, @{State=KH; Type=4}, @{State=KH; Type=5}}
    2 TX                        {@{State=TX; Type=6}, @{State=TX; Type=7}}
或者你可以直接转到Powershell 7