将 PSCustomObject 转换为哈希表
Converting PSCustomObject to Hashtable
我在从 json 负载转换一些数据时遇到了一些问题。我得到的最接近的是将它转换为如下所示的 PSCustomObject;
Fields
------
{@{short=true; title=Status; value=Status text.}, @{short=true; title=Type; value=Type text.}, @{short=false; title=Detail; value=This is some example detail text.}}
但是我需要将其转换为以下格式的哈希表。任何帮助将不胜感激。
Name Value
---- -----
short true
title Status
value Status text.
short true
title Type
value Type text.
short false
title Detail
value This is some example detail text.
谢谢,
如果我们假设您的 JSON 已转换为对象 $obj
,您可以执行以下操作以输出哈希数组 tables.
$obj.Fields | Foreach-Object {
$hash = [ordered]@{}
$_.PsObject.Properties | Foreach-Object {
$hash[$_.Name] = $_.Value
}
$hash
}
您的输出需要是散列 table 的数组,因为单个散列 table 不能有重复的键名。
我在从 json 负载转换一些数据时遇到了一些问题。我得到的最接近的是将它转换为如下所示的 PSCustomObject;
Fields
------
{@{short=true; title=Status; value=Status text.}, @{short=true; title=Type; value=Type text.}, @{short=false; title=Detail; value=This is some example detail text.}}
但是我需要将其转换为以下格式的哈希表。任何帮助将不胜感激。
Name Value
---- -----
short true
title Status
value Status text.
short true
title Type
value Type text.
short false
title Detail
value This is some example detail text.
谢谢,
如果我们假设您的 JSON 已转换为对象 $obj
,您可以执行以下操作以输出哈希数组 tables.
$obj.Fields | Foreach-Object {
$hash = [ordered]@{}
$_.PsObject.Properties | Foreach-Object {
$hash[$_.Name] = $_.Value
}
$hash
}
您的输出需要是散列 table 的数组,因为单个散列 table 不能有重复的键名。