首页 > 解决方案 > 如何在现有 json 文件中添加其他元素

问题描述

这就是我正在做的事情:

$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"

我怎样才能把它分开在不同的行上?

标签: jsonxmlpowershell

解决方案


因此,假设您的输入 xml 文件看起来像这样......

<application>
    <parameters>
        <applicationName>My Awesome App</applicationName>
        <!--my awesome comment-->
        <applicationTypeName>Mobile App</applicationTypeName>
        <applicationTypeVersion>299</applicationTypeVersion>
        <!--my other awesome comment-->
    </parameters>
</application>

这是我修改后的 PowerShell ...您不能使用if ($num = $_.Value -as [int])强制转换,因为它不适用于 0,因为它会被解释为错误。我更喜欢分解步骤并测试和检查每个步骤。此外,我使用InnerText了节点值而不是Value通常评估为,我不确定您的 xml 是什么样的Value$null

$fileXml = "./config.xml"
$fileJson = "./config.json"

$xmlContent = [Xml](Get-Content $fileXml)

# Transform the "Parameter" elements into a nested hashtable.
# Set any string values which are integers as [int] and strip out any comments in the xml file.
$parameters = [ordered]@{}
$nodes = $xmlContent.application.parameters.ChildNodes.Where{ $_.NodeType -ne 'Comment' }

foreach ($node in $nodes) {
    $parameter = $node.Name
    $value = $node.InnerText
    if ($value -match "^\d+$") { $value = [int] $value }
    $parameters.Add($parameter, @{ value = $value })
}

# if you need to add additional attributes, it's as simple as:

$parameters.Add("newParameter1", @{ value = "newValue1" })
$parameters.Add("newParameter2", @{ value = "newValue2" })

# 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     = $parameters
} | ConvertTo-Json | Out-File $fileJson

这是保存到 json 文件的输出:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "applicationName": {
      "value": "My Awesome App"
    },
    "applicationTypeName": {
      "value": "Mobile App"
    },
    "applicationTypeVersion": {
      "value": 299
    },
    "newParameter1": {
      "value": "newValue1"
    },
    "newParameter2": {
      "value": "newValue2"
    }
  }
}

推荐阅读