更新时间:2023-11-26 09:40:10
没有像cmdlet这样的Test-Json
,所以***的方法是将ConvertFrom-Json
cmdlet放在try ... catch
块内
There isn't a Test-Json
like cmdlet, so the best way is to put your ConvertFrom-Json
cmdlet inside a try ... catch
block
try {
$powershellRepresentation = ConvertFrom-Json $text -ErrorAction Stop;
$validJson = $true;
} catch {
$validJson = $false;
}
if ($validJson) {
Write-Host "Provided text has been correctly parsed to JSON";
} else {
Write-Host "Provided text is not a valid JSON string";
}