栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Powershell正确格式化JSON

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Powershell正确格式化JSON

由于您的原始json包含一个只有一个元素的数组,因此PowerShell会将其压缩为仅一个元素。如果在输出中希望再次将其作为数组,请使用rokumaru的好答案。

但是,PowerShell

ConvertTo-Json
不会生成格式精美的json,为此,我前段时间编写了一个辅助函数:

function Format-Json {    <#    .SYNOPSIS        Prettifies JSON output.    .DEscriptION        Reformats a JSON string so the output looks better than what ConvertTo-Json outputs.    .PARAMETER Json        Required: [string] The JSON text to prettify.    .PARAMETER Minify        Optional: Returns the json string compressed.    .PARAMETER Indentation        Optional: The number of spaces (1..1024) to use for indentation. Defaults to 4.    .PARAMETER AsArray        Optional: If set, the output will be in the form of a string array, otherwise a single string is output.    .EXAMPLE        $json | ConvertTo-Json  | Format-Json -Indentation 2    #>    [CmdletBinding(DefaultParameterSetName = 'Prettify')]    Param(        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]        [string]$Json,        [Parameter(ParameterSetName = 'Minify')]        [switch]$Minify,        [Parameter(ParameterSetName = 'Prettify')]        [ValidateRange(1, 1024)]        [int]$Indentation = 4,        [Parameter(ParameterSetName = 'Prettify')]        [switch]$AsArray    )    if ($PSCmdlet.ParameterSetName -eq 'Minify') {        return ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100 -Compress    }    # If the input JSON text has been created with ConvertTo-Json -Compress    # then we first need to reconvert it without compression    if ($Json -notmatch 'r?n') {        $Json = ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100    }    $indent = 0    $regexUnlessQuoted = '(?=([^"]*"[^"]*")*[^"]*$)'    $result = $Json -split 'r?n' |        ForEach-Object { # If the line contains a ] or } character,  # we need to decrement the indentation level unless it is inside quotes. if ($_ -match "[}]]$regexUnlessQuoted") {     $indent = [Math]::Max($indent - $Indentation, 0) } # Replace all colon-space combinations by ": " unless it is inside quotes. $line = (' ' * $indent) + ($_.TrimStart() -replace ":s+$regexUnlessQuoted", ': ') # If the line contains a [ or { character,  # we need to increment the indentation level unless it is inside quotes. if ($_ -match "[{[]$regexUnlessQuoted") {     $indent += $Indentation } $line        }    if ($AsArray) { return $result }    return $result -Join [Environment]::newline}

像这样使用它:

$json = Get-Content 'D:scripttest.json' -Encoding UTF8 | ConvertFrom-Json$json.yura.ContentManager.branch = 'test'# recreate the object as array, and use the -Depth parameter (your json needs 3 minimum)ConvertTo-Json @($json) -Depth 3 | Format-Json | Set-Content "D:scripttest1.json" -Encoding UTF8# instead of using '@($json)' you can of course also recreate the array by adding the square brackets manually:# '[{0}{1}{0}]' -f [Environment]::newline, ($json | ConvertTo-Json -Depth 3) | #        Format-Json | Set-Content "D:scripttest1.json" -Encoding UTF8


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616837.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号