json - 如何在现有 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"
我怎样才能把它分开在不同的行上?
解决方案
因此,假设您的输入 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"
}
}
}
推荐阅读
- c# - ReSharper规则在C#中缩进属性标签?
- javascript - Discord JS Bot - Gamedig 查询字符串和数字类型的信息
- scala - 在Scala中调用Kotlin的重载方法
- android - 在使用 ViewModel 更改数据的两个 RV 适配器之间创建拖放
- minecraft - 是否可以提高纸质服务器上的 TPS 以使红石计算更快?
- machine-learning - 逻辑回归和线性回归能否产生规模预测?
- c++ - 如何检查用户在字符串中输入正确的字符序列
- c - 在十六进制中写入整数与实数之间的区别
- linux - BPF 验证器拒绝代码:“无效的 bpf_context 访问”
- java - 如何在优先级队列中使用对,然后使用键作为优先级返回值