【问题标题】:How to convert integer xml value to JSON without quotes如何将整数 xml 值转换为不带引号的 JSON
【发布时间】: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


【解决方案1】:

XML 是基于文本,因此您从[xml] (System.Xml.XmlDocument) 文档中提取的任何值都将是字符串

引用Mathias R. Jessen's对该问题的评论: “XML specification 非常清楚这一点:只有字符串文字和引用可以用作属性值,并且必须使用单引号或双引号分隔它们。”

如果您希望这些值是其他类型,则必须执行显式转换

如果将这些转换后的值传递给ConvertTo-Json,那么如果引号的类型在 JSON 中不带引号(数字、nulltruefalse),则会自动省略引号

在手头的情况下,您可以使用-asconditional type conversion operator,有条件地将字符串转换为整数,如果可以这样解释:

# 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

以上结果如下:注意42 是如何未引用

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "Parameter1": {
      "value": "test1"
    },
    "parameter4": {
      "value": 42
    }
  }
}

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 2018-12-25
    • 2016-06-15
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2018-01-07
    • 2017-05-04
    相关资源
    最近更新 更多