【发布时间】:2021-02-16 07:21:04
【问题描述】:
这就是我正在做的:
$appParametersXml = [Xml] (Get-Content "$appParameterFilePath\$appParameterFile")
$parameterJsonFile = "$appParameterFilePath\$applicationName"+ "." + $jsonFileName
# Transform the "Parameter" elements into a nested hashtable.
# Convert any values that can be interpreted as [int] to [int] and strip out any comments in the xml file.
$hash = [ordered] @{}
$appParametersXml.Application.Parameters.ChildNodes | Where-Object {$_.NodeType -ne 'Comment'} | % {
$hash[$_.Name] = @{ value = if ($num = $_.Value -as [int]) { $num } else { $_.Value }
}
}
# Wrap the hashtable in a top-level hashtable and convert to JSON.
[ordered] @{
'$schema' = 'https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#'
contentVersion ='1.0.0.0'
parameters = $hash
} | ConvertTo-Json |Out-File $parameterJsonFile
Write-Host "The JSON File is: " $parameterJsonFile
在使用 XML 文件中的现有信息构建哈希表后,我需要在转换为 JSON 之前添加类似这样的附加参数值
"parameters": {
"applicationName": {
"value": "somevalue"
},
"applicationTypeName": {
"value": "somevalue"
},
"applicationTypeVersion": {
"value": "somevalue"
},
到目前为止,我所尝试的一切都赋予了我额外的价值。常规 XML 值正在以正确的方式进行转换,但我在转换之前添加的其他项目是这样出现的!
"applicationName": "somevalue"
我怎样才能把它分成不同的行?
【问题讨论】:
-
所以你不想得到别人的帮助?
-
:)。完全不是我说的!我试图标记某人,但我失败了。感谢大家的专业知识
-
好吧,我不会撒谎,他的帮助是最好的帮助之一。 :)
-
您可以直接编辑原始 xml(使用 xml 工具),使其成为最终形状,然后进行转换吗?还是根本不转换,只在修改后的xml上继续使用xml工具?
标签: json xml powershell