2017-06-25 52 views
0

我創建一個JSON格式的字符串構建JSON字符串時\輸出是正確的,只是所有qoutes已被替換的\字符,而不是」「正在改變在PowerShell中

{ 
    \$schema\: \https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#\, 
    \contentVersion\: \1.0.0.0\, 
    \parameters\: { 
\type\: \web\, 
\name\: \Cluster\, 
\hockeyAppToken\: \\, 
\hockeyAppId\: \\, 
\regionId\: \southcentralus\, 
\requestSource\: \IbizaAIExtension\ 
} 
} 

代碼生成Json

$fullDoc = ' 
{ 
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", 
    "contentVersion": "1.0.0.0", 
    "parameters": ' + $armsettings + '}' 

$json = $fullDoc | ConvertTo-Json -depth 100 
$json = $json.Replace("\r","") 
$json = $json.Replace("\n",([Environment]::NewLine)) 
$json = $json.Replace("@{","{") 
$json = $json.Trim() 
$json = $json.Replace($json.Substring(0,1),"")   

Write-Host $json 

有關如何解決此問題的任何想法?

這是正確格式的文檔

{ 
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", 
"contentVersion": "1.0.0.0", 
"parameters": { 
    "type": { 
     "value": "web" 
    }, 
    "name": { 
     "value": "Cluster" 
    }, 
    "hockeyAppToken": { 
     "value": "" 
    }, 
    "hockeyAppId": { 
     "value": "" 
    }, 
    "regionId": { 
     "value": "southcentralus" 
    }, 
    "requestSource": { 
     "value": "IbizaAIExtension" 
    } 
} 
} 
+0

爲什麼不'$ fullDoc | ConvertTo-Json | ConvertFrom-Json'? – Avshalom

+0

這到底是怎麼回事? @Avshalom – 4c74356b41

+0

@Avshalom。謝謝!這就像一個魅力:) –

回答

0
$fullDoc | ConvertTo-Json | ConvertFrom-Json 
+1

所以這到底是什麼做?它如何構建json? – 4c74356b41

+0

他的問題是,他試圖操縱與字符串替換工具等JSON時,他可以得到正確的結果與內置convertFrom-Json – Avshalom

+0

convertfrom-json不建立JSON,它創建一個對象 – 4c74356b41