首页 > 解决方案 > 如何在 JSON 输出中避免 \n

问题描述

尝试在PowerShell中将对象转换为字符串,输出\n在每行之后给出,如果我删除$result = $result | Out-String它会抛出

无法将“System.Object[]”转换为参数“Value”所需的类型“System.String”。不支持指定的方法。

$result = @(  )

$result = $result | Out-String

foreach ($point in $j) {
    $seconds = [long]$point.Time
    $point.Time = Get-EpochDate $seconds

    $result += "`n" + $point.Time
}

输出是

[
    {
        "key": "Time",
        "value": "\"\\n07/30/2019 17:34:16\\n07/29/2019 15:23:20\\n07/29/2019 13:54:05\""
    }
]

期待

[
    {
        "key": "Time",
        "value": "07/30/2019 17:34:1,
                  07/29/2019 15:23:20,
                   07/29/2019 13:54:05"
    }
]

标签: jsonpowershell

解决方案


推荐阅读