【问题标题】:Converting PSCustomObject to Hashtable将 PSCustomObject 转换为 Hashtable
【发布时间】:2021-02-25 18:29:33
【问题描述】:

我在从 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.

谢谢,

【问题讨论】:

标签: powershell hashtable pscustomobject


【解决方案1】:

如果我们假设您的 JSON 已转换为对象 $obj,您可以执行以下操作以输出哈希表数组。

$obj.Fields | Foreach-Object {
    $hash = [ordered]@{}
    $_.PsObject.Properties | Foreach-Object { 
        $hash[$_.Name] = $_.Value
    }
    $hash
}

您的输出需要是一个哈希表数组,因为单个哈希表不能有重复的键名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2011-08-01
    • 2014-08-14
    相关资源
    最近更新 更多