[英]JSON Array in PowerShell
Am trying to generate the below JSON string in PowerShell: 我试图在PowerShell中生成以下JSON字符串:
[
{
"FirstName": "Test",
"LastName": "Account2"
}
]
The code I have at the moment in PowerShell is: 我在PowerShell中的代码是:
$Body = @{
@{
FirstName='Test'
LastName='Account2'
}
}
The error I get with this is: Missing '=' operator after key in hash literal. 我得到的错误是:在哈希文字中键入后缺少'='运算符。
The outer []
is a list in JSON and you're trying to use @{}
, which is a hashtable in PowerShell. outer []
是JSON中的列表,您尝试使用@{}
,这是PowerShell中的哈希表。 Use @()
which is an array (list) in PowerShell: 在PowerShell中使用@()
这是一个数组(列表):
$Body = @(
@{
FirstName='Test'
LastName='Account2'
}
)
ConvertTo-Json -InputObject $Body
(and I use -InputObject instead of piping, because PowerShell is obsessed with flattering lists, and drops the list otherwise). (我使用-InputObject而不是管道,因为PowerShell很喜欢讨人喜欢的列表,否则会删除列表)。
I had some problems adding new items to the Powershell list. 我在Powershell列表中添加新项目时遇到了一些问题。 I tried to add an item using the .add() function. 我尝试使用.add()函数添加项目。 This returned the error "Collection was of a fixed size."
这返回了错误"Collection was of a fixed size."
Adding items to the list is just simple by using the += symbol. 使用+ =符号将项添加到列表中非常简单。
$Body += @{FirstName='Test2'; LastName='Account3'}
That simple. 那么简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.