使用 PowerShell 创建单元素 Json 数组对象 [英] Create a Single-Element Json Array Object Using PowerShell

查看:49
本文介绍了使用 PowerShell 创建单元素 Json 数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 powershell 5.1 脚本,它导入一个 csv 文件,处理一些数据,然后使用脚本末尾的 ConvertTo-Json cmdlet 将数据对象转换为 json 格式.我遇到的问题;在我的一个字段中,我需要为带括号的对象属性创建一个数组.所以我需要对象像:

I have a powershell 5.1 script that I've created that imports a csv file, manipulates some of the data, then converts the object of data to json format using the ConvertTo-Json cmdlet at the end of the script. The problem that I'm running into; in one of my fields I need to create a single array for the object property with brackets. So I need the object to be like:

"PersonGroups":[
              {
                "Name":"test Name",
                "Id": 3433
              }
            ]

这是对函数的调用:

 $_.PersonGroups = Set-DataHash -InputObject $_

以下是我的代码:

function Set-DataHash{
param(
    [psobject] $InputObject
)

$customObject = [psobject]@{
   Name = "Test"
   Id = 78888
}

$customArray = @($customObject)

return $customArray

}

当然,如果数组中有多个对象,它就可以正常工作;但由于它只是一个对象 ConvertTo-Json 使它成为一个对象.有什么建议吗?

Of course if I have more than one object in the array it works fine; but since it's only one object ConvertTo-Json makes it a single object. Any suggestions on what to do?

推荐答案

怎么样:

 $_.PersonGroups = @(Set-DataHash -InputObject $_)

您不需要回报".

这篇关于使用 PowerShell 创建单元素 Json 数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆