如果我有像这样的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,但我还没有完全弄清楚如何实现它。
https://stackoverflow.com/questions/56367507
复制相似问题