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

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

问题描述

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

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天全站免登陆