0

My Services Returns a Json string, which is stored in Database and later retrieved via a Scheduled Task as a "Command Argument".

string[] args = Environment.GetCommandLineArgs(); 

And the Result is something like below...

   args[0] =  {Name:Khan, Address: Ny} // no longer a valid json object

As you can see it does not have any qoutes around property names, so I cant really serialize/deserialize it into a c# object. so how do I convert that string to something like below

{"Name":"Khan", "Address": "Ny"}

Javascript has Stringify method, how about to get this done in c#?

highwingers
  • 1,649
  • 4
  • 21
  • 39
  • 2
    May be you can fix your service to return a valid string? – Vsevolod Goloviznin Feb 25 '15 at 08:03
  • Please, clarify your question. If your service returns something in incorrect format - why don't just fix your service? – Andrey Korneyev Feb 25 '15 at 08:04
  • Like @VsevolodGoloviznin said: http://stackoverflow.com/questions/2086666/how-do-i-return-clean-json-from-a-wcf-service – flayn Feb 25 '15 at 08:04
  • How does your service create this output? I think, there should be a library available for most languages to create proper JSON. Which, in turn, could be parsed at the client side. – Hermann Schachner Feb 25 '15 at 08:04
  • it returns a valid string, but problem is I am getting this string VIA a Environment.GetCommandLineArgs, and GetCommandLineArgs removes all the Quotes from string. – highwingers Feb 25 '15 at 08:05
  • @highwingers - That sounds like your real question, have you looked into how to return a string with quotes as an arg? [i.e something like this](http://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c-sharp)? – Sayse Feb 25 '15 at 08:07
  • I made a Edit. please read – highwingers Feb 25 '15 at 08:07
  • really a Negative by someone? – highwingers Feb 25 '15 at 08:14
  • Are all parameters always going to be strings? In other words, is every element always going to need quotes – Sayse Feb 25 '15 at 08:22

2 Answers2

1

As written here: Getting raw (unsplit) command line in .NET you can probably try with Environment.CommandLine. Note that it does include the name of your program that you'll have to remove.

You can split it with something like:

string str = Environment.CommandLine;

if (str.StartsWith("\"") && str.Length > 1) {
    int ix = str.IndexOf("\"", 1);

    if (ix != -1) {
        str = str.Substring(ix + 1).TrimStart();
    }
} else {
    int ix = str.IndexOf(" ");

    if (ix != -1) {
        str = str.Substring(ix + 1).TrimStart();
    }
}

This handles both cases that the exe file is quoted:

"MyProgram.exe" somearguments

and that the exe file is unquoted

MyProgram.exe somearguments

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
1

Disclaimer: It would be much better to fix the input...

If you really did need to parse it, you can just replace the separators with an equivalent version that has quotes around it. This will only ever work if every element is a string however that needs quotes..

var str = "{Name:Khan, Address: Ny}";
Console.WriteLine(str.Replace("{", "{\"")
                     .Replace("}", "\"}")
                     .Replace(",", "\", \"")
                     .Replace(":", "\":\""));

Output

{"Name":"Khan", " Address":" Ny"}

Example

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 1
    this will break if there are strings that contain the **,** (comma). So if someone has as the address **Street Something, 1** there will be a problem – xanatos Feb 25 '15 at 08:30
  • @xanatos - Very good point, Im not a fan of massaging data, and it would be very difficult to parse the string correctly at all after the fact.. (Only real way I could think of is a backwords looking regex that looks for the last comma before a semicolon but even this isn't fool-proof) – Sayse Feb 25 '15 at 08:31