使用powershell和Im試圖將其轉換爲JSON,但問題是需要將所有斜線加倍。所以\
需要變成\\
和\\
需要在輸出中被\\\\
取代。即時通訊使用PowerShell中使用以下腳本:使用powershell輸出斜槓
$array = @()
echo "{"; echo '"data":['; (get-counter "\Processor(*)\% Idle Time").CounterSamples.Path | %{ $array += '{ "{#CPUID}":"'+ $_.TrimStart("$LowerCaseComputerName") }
for($i = 0; $i -lt $array.Length - 1; $i++){ $array[$i] + '"},' }
$array[$array.Length - 1] + '"}'
echo "]" ; echo "}";
和輸出即時得到是正確的,除了反斜槓需要加倍:
{
"data":[
{ "{#CPUID}":"\\aaronmartinez\processor(0)\% idle time"},
{ "{#CPUID}":"\\aaronmartinez\processor(1)\% idle time"},
{ "{#CPUID}":"\\aaronmartinez\processor(2)\% idle time"},
{ "{#CPUID}":"\\aaronmartinez\processor(3)\% idle time"},
{ "{#CPUID}":"\\aaronmartinez\processor(4)\% idle time"},
{ "{#CPUID}":"\\aaronmartinez\processor(5)\% idle time"},
{ "{#CPUID}":"\\aaronmartinez\processor(6)\% idle time"},
{ "{#CPUID}":"\\aaronmartinez\processor(7)\% idle time"},
{ "{#CPUID}":"\\aaronmartinez\processor(_total)\% idle time"}
]
}
它只是一個字符串,基本上不會捕獲字符串的迴應,然後做一個簡單的字符串替換'' - > \\''? – 2014-10-06 21:43:44