Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Wrap dynamic string variables in double quoutes

I have static body which works great. But how I can wrap the same dynamic strings with double quotes – >""

Static Body

     var body = @"{" + "\n" +
        @"  ""profile"": {" + "\n" +
        @"    ""firstName"": ""Isaac1""," + "\n" +
        @"    ""lastName"": ""Brock1""," + "\n" +
        @"    ""email"": ""isaac1.brock@example.com""," + "\n" +
        @"    ""login"": ""isaac1.brock@example.com""," + "\n" +
        @"    ""mobilePhone"": ""555-415-1337""" + "\n" +
        @"  }" + "\n" +
        @"}";

Dyanmic Content (FirstName, LastName, Email, MobilePhone should all be wrapped with "")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var body1 = @"{" + "\n" +
                        @"  ""profile"": {" + "\n" +
                        @"    ""firstName"": "+ firstName + "," + "\n" +
                        @"    ""lastName"": "+lastName+"," + "\n" +
                        @"    ""email"": "+email+"," + "\n" +
                        @"    ""login"": "+email+"," + "\n" +
                        @"    ""mobilePhone"": "+mobilePhone+"" + "\n" +
                        @"  }" + "\n" +
                        @"}";

>Solution :

you can wrap a variable using string escape syntax " \ ""

var body1 = @"{" + "\n" +
                        @"  ""profile"": {" + "\n" +
                        @"    ""firstName"": " + "\"" + firstName + "\"" + "," + "\n" +
                        @"    ""lastName"": " + "\"" + lastName + "\"" + "," + "\n" +
                        //..... 
                        @"  }" + "\n" +
                        @"}";

but IMHO it is more safe to create a json object and after this to serialize it to a json string , using Newtonsoft.Json for example

var body = new JObject {
      ["profile"]=new JObject {
      ["firstName"] = firstName,
      ["lastName"] = lastName
      //...
    }
    }.ToString();

or you can replace JObject with an anonymos class and serialize after creaing.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading