如何将整数xml值转换为不带引号的JSON [英] How to convert integer xml value to JSON without quotes
问题描述
因此,我的xml如下所示,其中参数4为整数,其余为字符串参数.问题在于XML不允许我在不带引号的情况下放置整数值(它不喜欢这样).
So, I have my xml like shown below with Parameter 4 being an integer and the rest being string parameters. The problem is that XML doesn't allow me to put the integer value without quotes (it doesn't like that).
当我将其转换为JSON时,我希望整数值不带引号.
When I convert it to JSON, I want the integer value to come over without the quotes.
<?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来帮助我)
I have a nested hash table (Thanks @mklement0 for assisting me with that)
$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
)也位于引号内.有没有办法指定一个特定值是整数?
The output in the JSON file is correct, however, the last value (42
) is also coming over within quotes. Is there a way to specify that one specific value is an integer?
在此先感谢您的帮助:)
Thank you in advance for your help :)
推荐答案
XML是基于文本的,因此您从[xml]
( System.Xml.XmlDocument
)文档为 strings .
引用 Mathias R. Jessen的评论: " XML规范非常清楚:仅字符串文字和引用可以用作属性值,并且必须使用单引号或双引号对它们进行定界.
To quote from Mathias R. Jessen's comment on the question: "The XML specification is pretty clear about this: Only string literals and references can be used as attribute values, and they must be delimited using single- or double-quotes."
如果您希望这些值是其他类型,则必须执行显式转换 .
If you want those values to be other types, you must perform explicit conversions.
将此类转换后的值传递给ConvertTo-Json
时,如果引号的类型在JSON中未用引号表示(数字,null
,true
和false
)
Passing such converted values to ConvertTo-Json
will then automatically omit the quotes if they are of a type that is represented unquoted in JSON (numbers, null
, true
and false
)
在当前情况下,您可以使用-as
,即条件类型转换运算符,用于将字符串有条件地转换为整数, if 可以解释为:
In the case at hand, you can use -as
, the conditional type conversion operator, to conditionally convert a string to an integer, if it can be interpreted as such:
# Sample XML input.
# Note the two "Value" attribute values, "test1" and "42".
[xml] $xmlDoc = @'
<?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="parameter4" Value="42" />
</Parameters>
</Application>
'@
# Transform the "Parameter" elements into a nested hashtable.
# Convert any values that can be interpreted as [int] to [int].
$hash = [ordered] @{}
$xmlDoc.Application.Parameters.ChildNodes | ForEach-Object {
$hash[$_.Name] = @{
# Convert the text value to an [int], if it can be parsed as such.
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
上面的代码产生了以下内容:请注意
The above yields the following: note how 42
is unquoted:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"Parameter1": {
"value": "test1"
},
"parameter4": {
"value": 42
}
}
}
这篇关于如何将整数xml值转换为不带引号的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!