gpt4 book ai didi

json - 无法将数组中的 PSCustomObjects 正确转换回 JSON

转载 作者:行者123 更新时间:2023-12-01 05:28:37 28 4
gpt4 key购买 nike

我正在尝试将 JSON 文件提取到 Powershell 中,将 JSON block 附加到现有节点(组件),然后将 PSCustomObject 转换回 JSON 并保存文件。我正在使用的 JSON 看起来类似于图 1。

如您在我的代码中所见,我运行 ConvertTo-Json 将数据转换为 PSCustomObject,然后将一个新对象附加到组件节点。如果我查看对象,在这种情况下 $configFile 一切看起来都很好,但是当我将组件节点中的项目转换回 JSON 时,将被视为字符串,而不是评估为 JSON(请参阅最后一个片段)。我想这是因为 ConvertTo-JSON 按字面意思对待数组,但不是 100% 确定。

如果有人可以建议如何确保组件节点中的 PSCustomObjects 正确转换回 JSON,我将不胜感激,谢谢。

图 1 - 原始 JSON:

{
"EngineConfiguration": {
"PollInterval": "00:00:15",
"Components": [
{
"Id": "ApplicationEventLog",
"FullName": "AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"LogName": "Application",
"Levels": "1"
}
},
{
"Id": "SystemEventLog",
"FullName": "AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"LogName": "System",
"Levels": "7"
}
}
],
"Flows": {
"Flows":
[
"(ApplicationEventLog,SystemEventLog),CloudWatchLogs"
]
}
}
}

图 2 - 我的代码:

#Requires -Version 3.0

$configFile = "C:\Program Files\Amazon\EC2ConfigService\Settings\AWS.EC2.Windows.CloudWatch.json"
$configToPSObject = ConvertFrom-Json "$(Get-Content $configFile)"

$configToPSObject.EngineConfiguration.Components += New-Object -Type PSObject -Property ([ordered]@{
"Id" = "IISRequestQueueSize"
"FullName" = "AWS.EC2.Windows.CloudWatch.PerformanceCounterComponent.PerformanceCounterInputComponent,AWS.EC2.Windows.CloudWatch"
"Parameters" = [PSCustomObject]@{
"CategoryName" = "HTTP Service Request Queues"
"CounterName" = "CurrentQueueSize"
"InstanceName" = "_Total"
"MetricName" = "IISRequestQueueSize"
"Unit" = ""
"DimensionName" = ""
"DimensionValue" = ""
}
})

$configJson = ConvertTo-Json -Depth 5 $configToPSObject


Set-Content -Path $configFile -Value $configJson

图 3 - JSON 输出:

{
"EngineConfiguration": {
"PollInterval": "00:00:15",
"Components": [
"@{Id=ApplicationEventLog; FullName=AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}",
"@{Id=SystemEventLog; FullName=AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}",
"@{Id=IISRequestQueueSize; FullName=AWS.EC2.Windows.CloudWatch.PerformanceCounterComponent.PerformanceCounterInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}"
],
"Flows": {
"Flows":
"(ApplicationEventLog,SystemEventLog),CloudWatchLogs"
}
}
}

如果我增加深度说,8 或更多,JSON 如下所示:

{
"EngineConfiguration": {
"PollInterval": "00:00:15",
"Components": [
"@{Id=ApplicationEventLog; FullName=AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}",
"@{Id=SystemEventLog; FullName=AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}",
"Id": "IISRequestQueueSize",
"FullName": "AWS.EC2.Windows.CloudWatch.PerformanceCounterComponent.PerformanceCounterInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"CategoryName": "HTTP Service Request Queues",
"CounterName": "CurrentQueueSize",
"InstanceName": "_Total",
"MetricName": "IISRequestQueueSize",
"Unit": "",
"DimensionName": "",
"DimensionValue": ""
}
}
],
"Flows": {
"Flows": "(ApplicationEventLog,SystemEventLog),CloudWatchLogs"
}
}
}

最佳答案

ConvertTo-Json cmdlet 也有一个 Depth参数,超出该参数的对象将被 toString() 处理,而不是通过递归进行更深入的处理。因此,只需将该参数设置为您所拥有的对象的最大深度,就可以生成正确格式的 JSON。

$configJson = ConvertTo-Json $configToPSObject -Depth 8 
# your JSON has depth of 5, get some extra

关于json - 无法将数组中的 PSCustomObjects 正确转换回 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38742767/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com