ConvertFrom-Json:无效的 JSON 原语
ConvertFrom-Json : Invalid JSON primitive
我正在尝试在 azure vsts 发布管道中将此命令作为部署后操作内联脚本执行:
powershell -command "&{
$json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
$data = '{"allowedUrls" : [{ "host" : "someUrl","protocol" : "https"},{ "host":"localhost:44394","protocol" : "https"}]}' | ConvertFrom-Json;
$json.allowedUrls = $data.allowedUrls;
$json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json';
}"
当我在 powershell 中 运行 它在本地工作,但在 azure 上我得到:
Standard error from script:
2020-06-09T08:27:08.5939074Z ConvertFrom-Json : Invalid JSON primitive: someUrl.
2020-06-09T08:27:08.5939545Z At line:1 char:286
2020-06-09T08:27:08.5940773Z + ... host:localhost:44394,protocol : https}]}' | ConvertFrom-Json; Write- ...
2020-06-09T08:27:08.5941450Z + ~~~~~~~~~~~~~~~~
2020-06-09T08:27:08.5942156Z + CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentEx
2020-06-09T08:27:08.5942641Z ception
2020-06-09T08:27:08.5943137Z + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Co
2020-06-09T08:27:08.5943649Z mmands.ConvertFromJsonCommand
可能出了什么问题?
当像那样转义字符串时 $data = '{
"allowedUrls" : [{
"host" :
"someUrl",
"protocol" :
"https"},{
"host":
"localhost:44394",
"协议" :
"https"}]}' | ConvertFrom-Json;
我得到了
Standard error from script:
2020-06-09T09:13:30.3130099Z ConvertFrom-Json : Invalid object passed in, ':' or '}' expected. (4): [{
2020-06-09T09:13:30.3131022Z `host` : `someUrl`,`protocol` : `https`},{ `host`:`localhost:44394`,`protocol`
2020-06-09T09:13:30.3131695Z : `https`}]
2020-06-09T09:13:30.3132284Z At line:1 char:175
2020-06-09T09:13:30.3133099Z + ... :`localhost:44394`,`protocol` : `https`}]' | ConvertFrom-Json; $json. ...
这是解决方案:
powershell -command "&{
$json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
$data = '$(additionalAllowedUris)' | ConvertFrom-Json;
$json.additionalAllowedUris= $data.SyncRoot;
$json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json';
}"
管道中的变量:
[{ \"host\" : \"someUrl\",\"protocol\" : \"https\"},{ \"host\":\"localhost:44394\",\"protocol\" : \"https\"},{ \"host\":\"someUrl2\",\"protocol\" : \"https\"}]
我正在尝试在 azure vsts 发布管道中将此命令作为部署后操作内联脚本执行:
powershell -command "&{
$json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
$data = '{"allowedUrls" : [{ "host" : "someUrl","protocol" : "https"},{ "host":"localhost:44394","protocol" : "https"}]}' | ConvertFrom-Json;
$json.allowedUrls = $data.allowedUrls;
$json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json';
}"
当我在 powershell 中 运行 它在本地工作,但在 azure 上我得到:
Standard error from script:
2020-06-09T08:27:08.5939074Z ConvertFrom-Json : Invalid JSON primitive: someUrl.
2020-06-09T08:27:08.5939545Z At line:1 char:286
2020-06-09T08:27:08.5940773Z + ... host:localhost:44394,protocol : https}]}' | ConvertFrom-Json; Write- ...
2020-06-09T08:27:08.5941450Z + ~~~~~~~~~~~~~~~~
2020-06-09T08:27:08.5942156Z + CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentEx
2020-06-09T08:27:08.5942641Z ception
2020-06-09T08:27:08.5943137Z + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Co
2020-06-09T08:27:08.5943649Z mmands.ConvertFromJsonCommand
可能出了什么问题?
当像那样转义字符串时 $data = '{
"allowedUrls" : [{
"host" :
"someUrl",
"protocol" :
"https"},{
"host":
"localhost:44394",
"协议" :
"https"}]}' | ConvertFrom-Json;
我得到了
Standard error from script:
2020-06-09T09:13:30.3130099Z ConvertFrom-Json : Invalid object passed in, ':' or '}' expected. (4): [{
2020-06-09T09:13:30.3131022Z `host` : `someUrl`,`protocol` : `https`},{ `host`:`localhost:44394`,`protocol`
2020-06-09T09:13:30.3131695Z : `https`}]
2020-06-09T09:13:30.3132284Z At line:1 char:175
2020-06-09T09:13:30.3133099Z + ... :`localhost:44394`,`protocol` : `https`}]' | ConvertFrom-Json; $json. ...
这是解决方案:
powershell -command "&{
$json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
$data = '$(additionalAllowedUris)' | ConvertFrom-Json;
$json.additionalAllowedUris= $data.SyncRoot;
$json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json';
}"
管道中的变量:
[{ \"host\" : \"someUrl\",\"protocol\" : \"https\"},{ \"host\":\"localhost:44394\",\"protocol\" : \"https\"},{ \"host\":\"someUrl2\",\"protocol\" : \"https\"}]