powershell Convert-tojson 未给出最佳结果

标签 powershell

我有一个powershell问题,我需要将这段代码转换为Json

$Dimensions = @(
    @{
        name  = 'num_vm'
        value = '100'
    },
    @{
        name  = 'days'
        value = '10'
    },
    @{
        name  = 'months'
        value = '12'
    }
)

For ($i=0;$i -lt $Dimensions.length;$i++)
{
 $bodymessage = @{'resourceId' = $resourceUsageId; 'quantity' = "$Dimensions[$i].value"; 'dimension' = "$Dimensions[$i].name"; 'effectiveStartTime' = $lastHourMinusFiveMinutes; 'planId' = 'plan_meter' }
    $body += @{
          request = 
            @($bodymessage)} `
              |ConvertTo-Json -Depth 5
  
                       
   

}

$body

我希望它能够根据维度输入动态创建 JSON 输出, 但我得到的结果是这样的

{
    "request":  [
                    {
                        "resourceId":  null,
                        "quantity":  "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[0].value",
                        "planId":  "plan_meter",
                        "dimension":  "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[0].name",
                        "effectiveStartTime":  null
                    }
                ]
}{
    "request":  [
                    {
                        "resourceId":  null,
                        "quantity":  "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[1].value",
                        "planId":  "plan_meter",
                        "dimension":  "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[1].name",
                        "effectiveStartTime":  null
                    }
                ]
}{
    "request":  [
                    {
                        "resourceId":  null,
                        "quantity":  "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[2].value",
                        "planId":  "plan_meter",
                        "dimension":  "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[2].name",
                        "effectiveStartTime":  null
                    }
                ]
}

如何摆脱 System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[0].value? 并以实际值(value)显示?

最佳答案

主要问题是如何在字符串中分配哈希表元素值。通过删除引号来修复它,如下所示(格式化多行以提高可读性):

$bodymessage = @{
  resourceId = $resourceUsageId;
  quantity = $Dimensions[$i].value; # Removed the quotes here
  dimension = $Dimensions[$i].name; # Removed the quotes here
  effectiveStartTime = $lastHourMinusFiveMinutes;
  planId = 'plan_meter';
}

Note: I've also removed the single-quotes ' around the key names since they are not needed in this context. You only need to quote key names if non-alphanumeric characters are part of the key name, and you are assigning the key name as a literal string.


为什么会发生这种情况?

发生这种情况是因为字符串内变量扩展的工作方式。您最初拥有的以下字符串:

"$Dimensions[$i].value"

将看到 $Dimensions 得到展开(其 ToString() 函数被隐式调用),而不是 $Dimensions[$i].value 。这是因为变量解析从美元符号 $ 开始,并在第一个字符处停止,该字符通常不是变量名称的一部分。这将是最特殊的字符,除了冒号 :


但是,如果您尝试强制使用字符串值(也可能不会),则可以在字符串中执行子表达式来计算对象的成员,包括数组访问器和对象属性。您还可以执行更复杂的表达式,但对于下面的情况,您可以保留引号并使​​用 $() 来获得您想要的结果:

"$($Dimensions[$i].value)"

$() 告诉解析器“运行这个表达式并将结果放入字符串中”。同样,无论返回什么对象,当它被插入到最终字符串中时,都会隐式地运行 ToString() ,因此无论输出 $Dimensions[$i].value.ToString() 生成的将是您的字符串值。

关于powershell Convert-tojson 未给出最佳结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68984870/

相关文章:

git - 如何删除 posh-git 窗口标题开头的多余内容?

Powershell 大十六进制范围

Powershell - 展开子数组并重命名字段

powershell - 测试命令匹配结果为 false 或 true

bash - 如何在当前 PowerShell 工作目录中从 PowerShell 进入 Windows-Subsystem-for-Linux (ubuntu)

Powershell 写入错误不会填充 -ErrorVariable

PowerShell 测试连接 "On first response"

powershell - PowerShell启动之前的PSModulePath变量要求

python - 在Powershell中执行python脚本时获取 "ParserError: (:) [], ParentContainsErrorRecordException"

powershell - 按显示名称而不是 ID 备份 GPO - powershell