Pass a JSON string as a command line argument

后端 未结 6 1716
一个人的身影
一个人的身影 2021-01-04 18:19

I am trying to pass a json string to a C#-Program using Commandline.

The JSON-String looks like this:

{
    \"config\": {
        \"script\": {
             


        
相关标签:
6条回答
  • 2021-01-04 18:44

    Just send json value to commandline after catch value and replace it. It's work for me.

    args[1].Replace("{","{\"").Replace(":","\":\"").Replace(",","\",\"").Replace("}","\"}");
    
    0 讨论(0)
  • 2021-01-04 18:45

    Following on from @Selcuk Gurals post, here is a more complete answer:

    args[1].Replace("{", "{\"").Replace(":", "\":\"").Replace(",", "\",\"").Replace("}", "\"}").Replace(":\"[", ":[").Replace(":\"{", ":{").Replace("https\":\"", "https:").Replace("http\":\"", "http:").Replace("\":\"9", ":9").Replace("}\",", "},").Replace("]\",", "],").Replace("}\"}", "}}");
    

    This caters for things like embedded http/https and ports. My port number was in the 9000 region... So a regex solution would be better. But it improves on the former answer The value part of a JSON key/value pair can also be:

    1. another JSON object
    2. a list

    "key": {}, ....
    "key":[], ....
    
    0 讨论(0)
  • 2021-01-04 18:55

    Try to save the JSON object into a file, and pass the file as the argument to your application.

    @Wildcard27 : This is an actual use case in order to create Windows Tasks which was used for the faculty degree app. The JSON was just a simple serialization of a DTO that I was using.

    When you serialize the JSON, just save it into a blank file, giving it a proper name so that is unique.

        private string CreateTaskConfigurationFile(string taskName, EquipmentEventExtended eventData, string host)
            {
                List<Change> changes = new List<Change>
                {
                    new Change(MailConstants.EventName,eventData.EventName),
                    new Change(MailConstants.Deadline, eventData.DateTo.Value.ToShortDateString()),
                    new Change(MailConstants.EventDetails, eventData.EventDetails),
                    new Change(MailConstants.Link,$"{host}/Inventory/Details/{eventData.InventoryId}")
                };
    
                MailTaskModel mtm = new MailTaskModel
                {
                    Body = MailConstants.UpdateTemplate(MailConstants.TaskMailTemplate, changes),
                    Subject = "[Reminder] Upcoming Event needs your attention",
                    ToAddress = "abcdef@gmail.com",
                    IsHtml = true
                };
                var fileName = string.Format(@"E:\{0}.json", taskName);
                using (StreamWriter file = File.CreateText(fileName))
                {
                    JsonSerializer js = new JsonSerializer();
                    js.Serialize(file, mtm);
                }
                return fileName;
            }
    

    Then you provide the file path as an argument to the console application:

    static void Main(string[] args)
            {
                var configFilePath = args[0];
                var mailConfig = LoadConfigurationFile(configFilePath);
                MailManager manager = new MailManager(mailConfig.ToAddress, mailConfig.FromAddress,mailConfig.Subject, mailConfig.Body,mailConfig.IsHtml);
                manager.SendMail();
            }
            private static MailTaskModel LoadConfigurationFile(string configurationFilePath)
            {
                MailTaskModel mailConfig;
                using(var sr = new StreamReader(configurationFilePath))
                {
                    string json = sr.ReadToEnd();
                    mailConfig = JsonConvert.DeserializeObject<MailTaskModel>(json);
                }
                return mailConfig;
            }
    

    You can then use something like

    ConsoleApplication.exe -yourFilePath
    

    I've removed noisy check-ups for nulls and all that so that it's more clear.

    0 讨论(0)
  • 2021-01-04 18:57

    Declare it as a string with "" and escape the other " with \ and it should work.

    Command line:

    "{\"config\":{\"script\":{\"script_name\":\"test\",\"dir\":\"D:\\test\",\"destination\":\"M:\\neu\\test\",\"params\":\"/b /s /r:3 /w:5\"}}}"
    
    0 讨论(0)
  • 2021-01-04 19:01

    This should work:

    var jsonString = Environment.CommandLine;
    

    I tested it with the debugger like so:

            var jsonString = Environment.CommandLine;
            // (*) This correction makes it work, although it is pretty ugly:
            jsonString = jsonString.Split(new string[] { ".exe\" " }, StringSplitOptions.None)[1];
            var obj =   Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonString);
    

    Debugging with VS2015, and not modifying the json input (not even removing the line changes). I am using the same structure as your input:

        public class Script
        {
            public string script_name { get; set; }
            public string dir { get; set; }
            public string destination { get; set; }
            public string @params { get; set; }
        }
    
        public class Config
        {
            public Script script { get; set; }
        }
    
        public class RootObject
        {
            public Config config { get; set; }
        }
    

    About (*) => The problem with the deserialization is that the exe info is added in front of the command line with Environment.CommandLine and it "pollutes" the json like this: jsonString =

    "path\to\assembly\name.vshost.exe" {
        "config": {
            "script": {
                "script_name": "test",
                "dir": "D:\\test",
                "destination": "M:\\neu\\test",
                "params": "/b /s /r:3 /w:5"
            }
        }
    }
    

    If anybody has a prettier fix to this problem please let me know.

    0 讨论(0)
  • 2021-01-04 19:05

    Instead of looking at the "string[] args" you could use Environment.CommandLine.

    From MSDN https://msdn.microsoft.com/en-us/library/system.environment.commandline.aspx

    public static void Main() 
    {
       Console.WriteLine();
       //  Invoke this sample with an arbitrary set of command line arguments.
       Console.WriteLine("CommandLine: {0}", Environment.CommandLine);
    }
    

    // The example displays output like the following: // C:>env0 ARBITRARY TEXT //
    // CommandLine: env0 ARBITRARY TEXT

    0 讨论(0)
提交回复
热议问题