【问题标题】:ConvertFrom-Json : Invalid JSON primitiveConvertFrom-Json:无效的 JSON 原语
【发布时间】:2020-06-09 08:38:16
【问题描述】:

我正在尝试在 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 中运行它时它在本地工作,但在天蓝色上我得到了:

 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. ...

【问题讨论】:

  • 什么会在管道之前返回这个片段? ....host:localhost:44394,protocol : https}]}'
  • @Bhaal22 你是什么意思?我该如何检查?
  • 对不起,我看错了你的剧本。通过仔细阅读,我认为您应该在构建 $data 时避开引号。 $data = '{`"allowedUrls`" : [{ `"host`" : `"someUrl`",`"protocol`" : `"https`"},{ `"host`":`"localhost:44394`",`"protocol`" : `"https`"}]}' | ConvertFrom-Json;
  • @Bhaal22 检查更新的问题,没有帮助
  • 我让它这样工作:powershell -command "&{$data = '{"""protocol""" : """https"""}' | ConvertFrom-Json; Write-Output $data}

标签: powershell azure-devops azure-pipelines powershell-3.0


【解决方案1】:

这是解决方案:

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\"}]

【讨论】:

  • 好的。脚本现在略有不同。 $(additionalAllowedUris) 来自 appsetting.json 对吧?
  • @Bhaal22 来自 vsts 中的变量
  • 我明白了。那么使用它们确实更好。
猜你喜欢
  • 2019-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
相关资源
最近更新 更多