【发布时间】:2021-02-05 15:21:45
【问题描述】:
所以,我的 xml 如下所示,参数 4 为整数,其余为字符串参数。问题是 XML 不允许我将整数值放在不带引号的位置(它不喜欢那样)。
当我将它转换为 JSON 时,我希望整数值不带引号。
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="application" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<Parameters>
<Parameter Name="Parameter1" Value="test1" />
<Parameter Name="parameter2" Value="test2" />
<Parameter Name="parameter3" Value="test3" />
<Parameter Name="parameter4" Value="42" />
</Parameters>
</Application>
我有一个嵌套哈希表(感谢 @mklement0 帮助我)
$hash = [ordered] @{}
$appParametersXml.Application.Parameters.ChildNodes | % {
$hash[$_.Name] = @{ value = $_.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
JSON 文件中的输出是正确的,但是最后一个值 (42) 也在引号内。有没有办法指定一个特定的值是一个整数?
提前感谢您的帮助:)
【问题讨论】:
-
XML specification 对此非常清楚:只有字符串文字和引用可以用作属性值,并且必须使用单引号或双引号分隔。如果要将其转换为 JSON 作为整数值,则必须在调用
ConvertTo-Json之前将其显式转换为一个
标签: json xml powershell