在 powershell 中将其转换为 json 时添加方括号

Add square brackets while convert it to json in powershell

我正在从模板JSON 文件中获取参数并从 CSV 文件中读取数据,然后再次将其转换为 JSON。

TemplateJSON:
{
      "adhocUARs":[
        ""
      ],
      "rolefullpath": ""
    }

CSV File: 
RoleFullPath,ResourceName
FolderName\ABCD,ABCD User Account Resource

我得到这种格式的输出(没有方括号):

NEWJSON:
    {
    "adhocUARs":"ABCD User Account Resource",
     "rolefullpath": "xyz"  
    }

但我希望输出采用以下格式(带方括号):

 NEWJSON:
   {
      "adhocUARs":["ABCD User Account Resource"]    
      "rolefullpath": "xyz"     
   }

使用的代码:

$TemplateJSON = Convertfrom-json ([IO.File]::ReadAllText("TemplateJSON.json"))
$RoleFile = Import-CSV "CSVFile.csv" -Delimiter ','
[int]$Row = 0

Foreach ($Line in $RoleFile)
{    
    $Row = $Row + 1
    $NewJSON = $TemplateJSON
    $NewJSON.adhocUARs =  $Line.ResourceName
    $NewJSON.roleFullPath= $Line.RoleFullPath    

    $RolePath = "D:\DummyFolder\"
    $JSONPath = $RolePath + "patch.json"  
    Convertto-JSON $NewJSON | Out-file -Encoding "UTF8" $JSONPath
 }

我错过了什么?gr

所以在 JSON 中 [ ] 是一个数组。 当前,您将 $NewJSON.adhocUARs 作为字符串单个值。 一个简单的解决方案是:

$NewJSON.adhocUARs =  @($Line.ResourceName)