使用Powershell将JSON保存到具有漂亮打印的文件

时间:2016-05-04 16:00:56

标签: json powershell pretty-print

我在Windows 10上使用PowerShell。我需要以漂亮的缩进格式保存json文件。我尝试了以下代码的各种组合,但没有运气。

$url = example.com/api/someThingy $saveAs = people.json Invoke-RestMethod -Uri $url -Method Get -OutFile $saveAs;

JSON示例(我有可能删除了有所作为的内容。)

{"id":"123456","name":"Lorem", "content":null,"purpose":"<p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>\n<p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>","when":"<ul>\n<li>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</li>\n</ul>","Purpose":"<p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>\n<p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>"}

3 个答案:

答案 0 :(得分:2)

您可以使用ConvertTo-Json并保存。实施例

$url = "example.com/api/people"
$saveAs = people.json

Invoke-RestMethod -Uri $url -Method Get |
ConvertTo-Json |
Set-Content $saveAs

样品:

Invoke-RestMethod -Uri "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo" |
ConvertTo-Json |
Set-Content .\test.txt

答案 1 :(得分:1)

尝试以下方法:

$url = example.com/api/people
$saveAs = people.json
$jsonContent = Invoke-RestMethod -Uri $url -Method Get;

$prettyJsonContent = ($jsonContent | ConvertFrom-Json | ConvertTo-Json)

$prettyJsonContent *> $saveAs

上面的代码段使用PowerShell的ConvertFrom-JsonConvertTo-Json将JSON缩进为漂亮的打印。然后最后一行只是将JSON内容打印到当前目录中的文件名$saveAs

答案 2 :(得分:0)

通过Json.net运行就可以了。

$newtonSoftPath = "c:\mycode\packages\Newtonsoft.Json.8.0.3\lib\net40\Newtonsoft.Json.dll";
[System.Reflection.Assembly]::LoadFile($newtonSoftPath);

$url = "example.com/api/people"
$saveAs = "people.json";

$result = Invoke-WebRequest -Uri $url -Method Get;
#for json array
#$json = [Newtonsoft.Json.Linq.JArray]::Parse($result);

$json = [Newtonsoft.Json.Linq.JObject]::Parse($result);
$json.ToString() | Out-File $saveAs;