添加字段包装JSON-PowerShell

添加字段包装JSON-PowerShell,json,powershell,Json,Powershell,我有一个转换为json的数组。但我希望数组中的每个对象都被另一个字段包装 $Array = Field1; Field2 ----------------- Value11; Value12 Value21; Value22 如果我将该数组转换为JSON,则如下所示: Array [ { "Field1": "Value11", "Field2": "Value12" }, { "Field1": "Value21

我有一个转换为json的数组。但我希望数组中的每个对象都被另一个字段包装

$Array =

Field1; Field2
-----------------
Value11; Value12
Value21; Value22
如果我将该数组转换为JSON,则如下所示:

Array
[
    {
        "Field1":  "Value11",
        "Field2":  "Value12"
    },
    {
        "Field1":  "Value21",
        "Field2":  "Value22"
    }
]
Array
[
    {"NewWrapper":
        {
        "Field1":  "Value11",
        "Field2":  "Value12"
        }
    },
    {"NewWrapper":
        {
        "Field1":  "Value21",
        "Field2":  "Value22"
        }
    }
]
我希望它看起来像这样:

Array
[
    {
        "Field1":  "Value11",
        "Field2":  "Value12"
    },
    {
        "Field1":  "Value21",
        "Field2":  "Value22"
    }
]
Array
[
    {"NewWrapper":
        {
        "Field1":  "Value11",
        "Field2":  "Value12"
        }
    },
    {"NewWrapper":
        {
        "Field1":  "Value21",
        "Field2":  "Value22"
        }
    }
]
如何格式化源代码或json以实现此目的?

请尝试以下操作:

$Array | ForEach-Object { @{ NewWrapper=$_ } } | ConvertTo-Json
{NewWrapper=$\}
将每个输入对象包装在一个哈希表(
{…}
)中,该哈希表唯一的一个条目是输入对象(
$

converttojson
序列化此哈希表时,它将生成所需的输出


完整示例:

# Create sample input objects...
$Array = [pscustomobject] @{ Field1 = 'Value11'; Field2 = 'Value12' }, 
         [pscustomobject] @{ Field1 = 'Value21'; Field2 = 'Value12' }
# ... wrap them, and convert them to JSON.
$Array | ForEach-Object { @{ NewWrapper=$_ } } | ConvertTo-Json