首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConvertFrom-Json :无效的JSON原语

ConvertFrom-Json :无效的JSON原语
EN

Stack Overflow用户
提问于 2020-06-09 16:38:16
回答 1查看 1.1K关注 0票数 0

我正在尝试将此命令作为部署后操作行内脚本在azure vsts发布管道中执行:

代码语言:javascript
复制
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上我得到:

代码语言:javascript
复制
 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","protocol" :"https"}]}' | ConvertFrom-Json;这样的转义字符串出现时

我得到了

代码语言:javascript
复制
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. ...
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-09 18:11:26

这就是解决方案:

代码语言:javascript
复制
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';
    }"

和管道中的变量:

代码语言:javascript
复制
[{ \"host\" : \"someUrl\",\"protocol\" : \"https\"},{ \"host\":\"localhost:44394\",\"protocol\" : \"https\"},{ \"host\":\"someUrl2\",\"protocol\" : \"https\"}]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62278381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档