Convert to JSON with comments from PowerShell

前端 未结 3 489
逝去的感伤
逝去的感伤 2021-01-12 05:56

I have a very simple json and this code works for him:

function Get-CustomHeaders() {
   return Get-Content -Raw -Path $JsonName | ConvertFrom-Json
}
         


        
3条回答
  •  梦谈多话
    2021-01-12 06:10

    Here you have an example which can't be handled right by previous answers:

    {
    "url":"http://something" // note the double slash in URL
    }
    

    so here is regexp that solves also this problem.

    $configFile = $configFile -replace '(?m)(?<=^([^"]|"[^"]*")*)//.*' -replace '(?ms)/\*.*?\*/'
    

    IMPORTANT NOTE:

    Powershell 6.0+ can load JSON with comments in it.

提交回复
热议问题