我正在使用 PowerShell 将 CSV 文件转换为 JSON 格式。问题是 CSV 在列值周围加上双引号,甚至是整数。在转换过程中如何将它们转换为整数?
这是我用来将 *.csv 文件目录转换为 *.json 的脚本:
Get-ChildItem .\*.csv | Foreach-Object {
$basename = $_.BaseName
import-csv $_ | ConvertTo-Json -Compress | Foreach {$_ -creplace '"NULL"','null'} | Out-File ".\$basename.json"
}
这是我的源 CSV:
"Id","Name","Active"
"1","Test 1","1"
"2","Test 2","0"
"3","Test 3","1"
这是它的输出:
[
{"Id":"1","Name":"Test 1","Active":"1"}
{"Id":"2","Name":"Test 2","Active":"0"}
{"Id":"3","Name":"Test 3","Active":"1"}
]
我如何让它输出这个呢?:
[
{"Id":1,"Name":"Test 1","Active":1}
{"Id":2,"Name":"Test 2","Active":0}
{"Id":3,"Name":"Test 3","Active":1}
]