【发布时间】:2013-07-12 18:14:27
【问题描述】:
我正在使用 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}
]
【问题讨论】:
标签: json powershell csv powershell-3.0