几天前我就遇到了一个问题,现在在这里报告。我想将几个JSON文件导出到Excel电子表格中。JSON键应该形成标题,值应该列在标题下。不幸的是,我对Powershell一无所知,无法在其他线程的帮助下取得任何进展,因为我也不明白为什么有些东西会以它的方式工作或不工作。
json文件如下所示
{"dataCollection": [
{
"objectID": 000001,
"randomID": 123,
"desc": "The sky is blue",
"startTime": "2022-03-15T11:31:56.510",
"endTime": "2022-03-15T11:31:56.511",
"caseOne": true,
"caseTwo": false,
"caseThree": null
},
{
"objectID": 333222,
"randomID": 456,
"desc": "example",
"startTime": "2022-03-15T11:31:56.510",
"endTime": "2022-03-15T11:31:56.511",
"caseOne": false,
"caseTwo": true,
"caseThree": null
},
{
"objectID": 111111,
"randomID": 789,
"desc": "Mo-Fr 60% 20-24",
"startTime": "2022-03-15T11:31:56.510",
"endTime": "2022-03-15T11:31:56.511",
"caseOne": false,
"caseTwo": false,
"caseThree": null
}
]}
我当前的代码如下所示
$contentJson = Get-Content -Raw -Path $jsonInput | ConvertFrom-Json
$obj_list = $contentJson | Select-Object @{Name='Name';Expression={$_}}
$obj_list | Export-Csv $csvOutput -NoType -Delimiter "`t" -Encoding Unicode
(Get-Content -Path $csvOutput -Raw).replace('"','') | Set-Content -Path $csvOutput
这确实给予了我一个包含JSON信息的CSV,但是它是一个单元格一个单元格地传输的,我不知道如何创建标题。此外,这只在我删除JSON的第一行(在本例中为{“DataCollection”:)时才起作用,否则在Excel表中只会写入以下内容:@{ttDebugTage=系统对象[]}
我的目标是这样的:Excel:
这是我第一次使用Powershell,不幸的是我完全缺乏理解,所以我将感谢任何帮助。
2条答案
按热度按时间ej83mcc01#
zazmityj2#