【问题标题】:how to get json from passed string via command line c#如何通过命令行c#从传递的字符串中获取json
【发布时间】:2020-09-18 11:39:30
【问题描述】:

我这样做是有原因的。这是我下一步申请所需要的。我正在创建 Visual Studio 扩展,我需要它用于插件和项目之间的通信。

我在 Visual Studio 中将 json 字符串作为命令行参数传递。

当我从控制台加载该字符串时,它没有某些 json 字符,因此无法反序列化。

我作为 arg 粘贴到控制台的是:

{"lastModifiedDate":"2020-07-13T08:06:01Z","model":{"softwareSystems":[{"containers":[{"components":[{"location":0,"relationships":[{"sourceId":9,"destinationId":11,"technology":"","id":14,"name":null,"description":"","tags":"Relationship","tagsList":[]}],"id":9,"name":"system1 container1 component1","description":"","tags":"Element,Component","tagsList":[]},{"location":0,"relationships":[{"sourceId":10,"destinationId":9,"technology":"","id":16,"name":null,"description":"","tags":"Relationship","tagsList":[]}],"id":10,"name":"system1 container1 component2","description":"","tags":"Element,Component","tagsList":[]}],"location":0,"relationships":[{"sourceId":5,"destinationId":8,"technology":"","id":13,"name":null,"description":"","tags":"Relationship","tagsList":[]}],"id":5,"name":"system1 container1","description":"","tags":"Element,Container","tagsList":[]},{"components":[{"location":0,"relationships":[],"id":11,"name":"system2 container1 component1","description":"","tags":"Element,Component","tagsList":[]}],"location":0,"relationships":[{"sourceId":6,"destinationId":5,"technology":"","id":15,"name":null,"description":"","tags":"Relationship","tagsList":[]}],"id":6,"name":"system1 container2","description":"","tags":"Element,Container","tagsList":[]}],"location":0,"relationships":[{"sourceId":2,"destinationId":4,"technology":"","id":12,"name":null,"description":"","tags":"Relationship","tagsList":[]},{"sourceId":2,"destinationId":7,"technology":"","id":20,"name":null,"description":"","tags":"Relationship","tagsList":[]}],"id":2,"name":"Software System1","description":"My software system.","tags":"Element,Software System","tagsList":[]},{"containers":[{"components":[],"location":0,"relationships":[{"sourceId":7,"destinationId":6,"technology":"","id":17,"name":null,"description":"","tags":"Relationship","tagsList":[]}],"id":7,"name":"system2 container2","description":"","tags":"Element,Container","tagsList":[]},{"components":[],"location":0,"relationships":[{"sourceId":8,"destinationId":7,"technology":"","id":21,"name":null,"description":"","tags":"Relationship","tagsList":[]}],"id":8,"name":"system2 container1","description":"","tags":"Element,Container","tagsList":[]}],"location":0,"relationships":[],"id":4,"name":"Software System2","description":"","tags":"Element,Software System","tagsList":[]}],"people":[{"location":0,"relationships":[{"sourceId":1,"destinationId":2,"technology":"","id":3,"name":null,"description":"Uses","tags":"Relationship,Synchronous","tagsList":[]},{"sourceId":1,"destinationId":9,"technology":"","id":18,"name":null,"description":"","tags":"Relationship","tagsList":[]},{"sourceId":1,"destinationId":5,"technology":"","id":19,"name":null,"description":"","tags":"Relationship","tagsList":[]}],"id":1,"name":"User","description":"A user of my software system.","tags":"Element,Person","tagsList":[]}],"lastId":0},"views":{"systemContextViews":[{"softwareSystemId":2,"description":"An example of a System Context diagram.","configuration":{"styles":null},"elements":[{"id":4,"x":305,"y":300},{"id":1,"x":1255,"y":-5},{"id":2,"x":1190,"y":935}],"relationships":[{"id":3},{"id":12}]}],"containerViews":[{"softwareSystemId":2,"description":"","elements":[{"id":5,"x":110,"y":215},{"id":6,"x":1875,"y":715}],"relationships":[{"id":15}]},{"softwareSystemId":4,"description":"","elements":[{"id":8,"x":175,"y":224},{"id":7,"x":1845,"y":720}],"relationships":[{"id":21}]}],"componentViews":[{"containerId":5,"description":"","elements":[{"id":9,"x":120,"y":80},{"id":10,"x":1880,"y":1105},{"id":11,"x":130,"y":1000},{"id":1,"x":1760,"y":120}],"relationships":[{"id":14},{"id":16},{"id":18}]}]},"id":0,"name":"Demo workspace","description":"A demo workspace.","tags":"ElementBase,Workspace","tagsList":[]}

从控制台我得到

"{lastModifiedDate:2020-07-13T08:06:01Z,model:{softwareSystems:[{containers:[{components:[{location:0,relationships:[{sourceId:9,destinationId:11,technology:,id:14,name:null,description:,tags:Relationship,tagsList:[]}],id:9,name:system1 container1 component1,description:,tags:Element,Component,tagsList:[]},{location:0,relationships:[{sourceId:10,destinationId:9,technology:,id:16,name:null,description:,tags:Relationship,tagsList:[]}],id:10,name:system1 container1 component2,description:,tags:Element,Component,tagsList:[]}],location:0,relationships:[{sourceId:5,destinationId:8,technology:,id:13,name:null,description:,tags:Relationship,tagsList:[]}],id:5,name:system1 container1,description:,tags:Element,Container,tagsList:[]},{components:[{location:0,relationships:[],id:11,name:system2 container1 component1,description:,tags:Element,Component,tagsList:[]}],location:0,relationships:[{sourceId:6,destinationId:5,technology:,id:15,name:null,description:,tags:Relationship,tagsList:[]}],id:6,name:system1 container2,description:,tags:Element,Container,tagsList:[]}],location:0,relationships:[{sourceId:2,destinationId:4,technology:,id:12,name:null,description:,tags:Relationship,tagsList:[]},{sourceId:2,destinationId:7,technology:,id:20,name:null,description:,tags:Relationship,tagsList:[]}],id:2,name:Software System1,description:My software system.,tags:Element,Software System,tagsList:[]},{containers:[{components:[],location:0,relationships:[{sourceId:7,destinationId:6,technology:,id:17,name:null,description:,tags:Relationship,tagsList:[]}],id:7,name:system2 container2,description:,tags:Element,Container,tagsList:[]},{components:[],location:0,relationships:[{sourceId:8,destinationId:7,technology:,id:21,name:null,description:,tags:Relationship,tagsList:[]}],id:8,name:system2 container1,description:,tags:Element,Container,tagsList:[]}],location:0,relationships:[],id:4,name:Software System2,description:,tags:Element,Software System,tagsList:[]}],people:[{location:0,relationships:[{sourceId:1,destinationId:2,technology:,id:3,name:null,description:Uses,tags:Relationship,Synchronous,tagsList:[]},{sourceId:1,destinationId:9,technology:,id:18,name:null,description:,tags:Relationship,tagsList:[]},{sourceId:1,destinationId:5,technology:,id:19,name:null,description:,tags:Relationship,tagsList:[]}],id:1,name:User,description:A user of my software system.,tags:Element,Person,tagsList:[]}],lastId:0},views:{systemContextViews:[{softwareSystemId:2,description:An example of a System Context diagram.,configuration:{styles:null},elements:[{id:4,x:305,y:300},{id:1,x:1255,y:-5},{id:2,x:1190,y:935}],relationships:[{id:3},{id:12}]}],containerViews:[{softwareSystemId:2,description:,elements:[{id:5,x:110,y:215},{id:6,x:1875,y:715}],relationships:[{id:15}]},{softwareSystemId:4,description:,elements:[{id:8,x:175,y:224},{id:7,x:1845,y:720}],relationships:[{id:21}]}],componentViews:[{containerId:5,description:,elements:[{id:9,x:120,y:80},{id:10,x:1880,y:1105},{id:11,x:130,y:1000},{id:1,x:1760,y:120}],relationships:[{id:14},{id:16},{id:18}]}]},id:0,name:Demo workspace,description:A demo workspace.,tags:ElementBase,Workspace,tagsList:[]}"

正如你所看到的“{”,缺少双引号。

我如何从控制台获取字符串:

string[] commandLineArgs = Environment.GetCommandLineArgs();

我需要可以转换为相同形式的方法,因为字符串会不同

你知道如何解决我的问题吗?

【问题讨论】:

标签: c# json visual-studio json-deserialization


【解决方案1】:
c:\> myapp.exe "{\"asdf\":\"x yz\", \"xyz\": 3}"

为我工作(在 Win10 上,目前无法测试其他版本)。

使用\ 转义属于 JSON 字符串的所有双引号。然后用双引号将整个 json 对象括起来以将空格括起来,这样字符串就不会被多个参数分割。

编辑

作为替代方案,如果您负责创建和接收 json 字符串,则可以使用更“CMD 友好”的格式,例如 Base64。这样,您就不必在命令行上处理空格、引号或其他特殊字符

在生成端:

var someobject = new {key1 = "value1", key2 = 3};
var json = Newtonsoft.Json.JsonConvert.SerializeObject(someobject);
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
var base64 = System.Convert.ToBase64String(plainTextBytes);
// base64 will look something like
// eyJrZXkxIjogInZhbHVlMSIsICJrZXkyIjogM30= 
// depending on the formatting of the serialization

然后将 base64 作为参数传递给您的命令行。

C:\>someapp.exe eyJrZXkxIjogInZhbHVlMSIsICJrZXkyIjogM30=

在接收方做

var base64 = Environment.GetCommandlineArgs()[1];
var base64EncodedBytes = System.Convert.FromBase64String(base64);
var json = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
var someObject = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

【讨论】:

  • 我需要修复字符串的方法,因为控制台的输出会不同
  • 你负责生成原始的json字符串吗?如果是这样,我建议创建一个更“CMD 友好”的格式,例如对 json 字符串进行 base64 编码,将其作为参数传递给 CMD 并在反序列化 json 之前解码 base64
  • 否则,只需在 json 字符串中的每个 " 之前添加一个 \ 即可。尽管如果 json 字符串包含已经转义的引号,这可能会出现问题。一旦字符串在接收应用程序中,修复它就太晚了,因为关键信息已经丢失。
  • @derpischer 是的,我负责生成原始 json 字符串。我还没有尝试过 bas64 编码。
【解决方案2】:

要转义双引号(“),请使用它两次。所以当你将“”传递给命令时,它将接收为“。 例如将数据传递为: ConsoleAppTest.exe "{""lastModifiedDate"":""2020-07-13T08:06:01Z"",""model"":""softwareSystems""}"

请参考以下类似答案:

【讨论】:

  • 这对我不起作用。这些链接没有帮助。
  • 请尝试如下: >ConsoleAppTest.exe "{""lastModifiedDate"":""2020-07-13T08:06:01Z"",""model"":""softwareSystems"" }"
猜你喜欢
  • 1970-01-01
  • 2018-10-28
  • 2020-08-18
  • 2016-07-21
  • 1970-01-01
  • 2014-07-10
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多