如果我有这样的 JSON:
$inputJson = @"
{
"attachments" :
[
{
"name": "attachment.eml",
"attachment_url": "https://www.attachment1.com"
},
{
"name": "attachment.eml",
"attachment_url": "https://www.attachment2.com"
},
{
"name": "attachment.eml",
"attachment_url": "https://www.attachment3.com"
}
]
}
哪里有一个attachments
数组,并且该数组中的每个元素都具有相同的名称但不同的 URL,我如何更改所有名称以使其输出如下:
$outputJson = @"
{
"attachments" :
[
{
"name": "(attachment.eml)[1].eml",
"attachment_url": "https://www.attachment1.com"
},
{
"name": "(attachment.eml)[2].eml",
"attachment_url": "https://www.attachment2.com"
},
{
"name": "(attachment.eml)[3].eml",
"attachment_url": "https://www.attachment3.com"
}
]
}
"@
它重命名每个附件以防止重复名称,但它保留了 URL。
理想情况下,代码还将确保数组中也不存在任何新名称。因此,如果数组中已经有一个附件(attachment.eml)[1].eml
,它也会处理它。
我已经考虑过以某种方式使用Group-Object
,但我还没有完全弄清楚如何让它发挥作用。