将数组写入文件:
PS C:\Users\dharmatech\Downloads> 10, 20, 30 | Out-File 'test.txt'
重新读取数组并在其上调用ConvertTo-Json
:
PS C:\Users\dharmatech\Downloads> ConvertTo-Json (Get-Content .\test.txt)
结果不是数组的JSON版本...请参阅下面的结果。
如果在从文件读取的数组中的一个字符串上调用Get-Member
,您将看到字符串对象已使用少量NoteProperty
进行注释。我猜这解释了ConvertTo-Json
输出。
使用ConvertTo-JSON
在文件中获取阵列的JSON版本的好方法是什么?
[
{
"value": "10",
"PSPath": "C:\\Users\\dharmatech\\Downloads\\test.txt",
"PSParentPath": "C:\\Users\\dharmatech\\Downloads",
"PSChildName": "test.txt",
"PSDrive": {
"CurrentLocation": "Users\\dharmatech\\Downloads",
"Name": "C",
"Provider": "Microsoft.PowerShell.Core\\FileSystem",
"Root": "C:\\",
"Description": "",
"Credential": "System.Management.Automation.PSCredential",
"DisplayRoot": null
},
"PSProvider": {
"ImplementingType": "Microsoft.PowerShell.Commands.FileSystemProvider",
"HelpFile": "System.Management.Automation.dll-Help.xml",
"Name": "FileSystem",
"PSSnapIn": "Microsoft.PowerShell.Core",
"ModuleName": "Microsoft.PowerShell.Core",
"Module": null,
"Description": "",
"Capabilities": 52,
"Home": "C:\\Users\\dharmatech",
"Drives": "C D"
},
"ReadCount": 1
},
{
"value": "20",
"PSPath": "C:\\Users\\dharmatech\\Downloads\\test.txt",
"PSParentPath": "C:\\Users\\dharmatech\\Downloads",
"PSChildName": "test.txt",
"PSDrive": {
"CurrentLocation": "Users\\dharmatech\\Downloads",
"Name": "C",
"Provider": "Microsoft.PowerShell.Core\\FileSystem",
"Root": "C:\\",
"Description": "",
"Credential": "System.Management.Automation.PSCredential",
"DisplayRoot": null
},
"PSProvider": {
"ImplementingType": "Microsoft.PowerShell.Commands.FileSystemProvider",
"HelpFile": "System.Management.Automation.dll-Help.xml",
"Name": "FileSystem",
"PSSnapIn": "Microsoft.PowerShell.Core",
"ModuleName": "Microsoft.PowerShell.Core",
"Module": null,
"Description": "",
"Capabilities": 52,
"Home": "C:\\Users\\dharmatech",
"Drives": "C D"
},
"ReadCount": 2
},
{
"value": "30",
"PSPath": "C:\\Users\\dharmatech\\Downloads\\test.txt",
"PSParentPath": "C:\\Users\\dharmatech\\Downloads",
"PSChildName": "test.txt",
"PSDrive": {
"CurrentLocation": "Users\\dharmatech\\Downloads",
"Name": "C",
"Provider": "Microsoft.PowerShell.Core\\FileSystem",
"Root": "C:\\",
"Description": "",
"Credential": "System.Management.Automation.PSCredential",
"DisplayRoot": null
},
"PSProvider": {
"ImplementingType": "Microsoft.PowerShell.Commands.FileSystemProvider",
"HelpFile": "System.Management.Automation.dll-Help.xml",
"Name": "FileSystem",
"PSSnapIn": "Microsoft.PowerShell.Core",
"ModuleName": "Microsoft.PowerShell.Core",
"Module": null,
"Description": "",
"Capabilities": 52,
"Home": "C:\\Users\\dharmatech",
"Drives": "C D"
},
"ReadCount": 3
}
]
PS C:\Users\dharmatech\Downloads>
答案 0 :(得分:2)
这似乎有效:
10, 20, 30 | Out-File 'testfile1.txt'
convertto-json ([string[]](gc testfile1.txt))
[
"10",
"20",
"30"
]
或者,如果你想要它们作为[int]
10, 20, 30 | Out-File 'testfile1.txt'
convertto-json ([int[]](gc testfile1.txt))
[
10,
20,
30
]
答案 1 :(得分:2)
Get-Content有一个坏习惯,即通过大量额外信息扩展它输出的字符串。处理大文件时,这可能会导致内存膨胀。我希望Get-Content有一个开关,可以在需要时启用这些额外的属性,这种情况很少见。但是,由于这可能会破坏现有程序,因此请求一个剥去额外属性的参数更为可行。也许像ValueOnly
这样的东西?
要解决这些问题,您可以将其作为上面的mjolinor演示,但为了获得最佳性能,请下载到.NET:
ConvertTo-Json ([IO.File]::ReadAllLines("$pwd\test.txt"))
[
"10",
"20",
"30"
]
虽然对于除了非常大的文件之外的任何内容都不需要这样做,但这是一个很好的技巧。