0
return '{"response" : "' + responseMsg + '" , "isNewAUser" : "' + isNewUser + '"}'; 

is flagging as c# string too many characters in character literal..to do with single and double quotes yes but I want to return...

{
  "response": "This is a response Msg Eg",
  "isNewAUser": "false"
}
John
  • 3,965
  • 21
  • 77
  • 163
  • 1
    You can use escape sequence `\"` or `@` sign before `"` (treated as verbatim strings) for assembling JSON string. The most convenient way is using serializer like `JavaScriptSerializer().Serialize()` method or something like that. – Tetsuya Yamamoto Jan 03 '18 at 01:19
  • you might consider using a JSON seralizer. – Daniel A. White Jan 03 '18 at 01:20
  • If you don't use a JSON serializer as @DanielA.White suggests (which would be very helpful if you don't want to manually escape quotes for large strings), in your case the escaped string would be `return "{\"response\" : \" + responseMsg + \" , \"isNewAUser\" : \" + isNewUser + \"}";` – Keyur PATEL Jan 03 '18 at 01:26
  • @KeyurPATEL hanks for the reply but that returns {"response" : " + responseMsg + " , "isNewActiveDirectoryUser" : " + isNewActiveDUser + "} – John Jan 03 '18 at 01:31
  • @DanielA.White thanks for reply but I only have one message to return – John Jan 03 '18 at 01:31
  • stil it would get hairy if `responseMsg` contains a quote. – Daniel A. White Jan 03 '18 at 01:32
  • @TetsuyaYamamoto thanks for reply I went with string test = "{\"response\" : \" " + responseMsg + " \" , \"isNewUser\" : \" " + isNewUser + " \"}"; – John Jan 03 '18 at 01:33

2 Answers2

2

Serialize it

The ideal way is to use a serialization library, such as the one provided with NewtonSoft.Json:

var x = new { response = responseMessage, isNewAUser = isNewUser } ;
var s = JsonConvert.SerializeObject(x);
Console.WriteLine(s);

Output:

{"response":"Hello world!","isNewAUser":false}

Use string.Format:

If you insist on doing this with string manipulation, you can do it a few ways.

Normal string:

var template = "{{\"response\": \"{0}\",\"isNewAUser\": \"{1}\"}}";
var s = string.Format(template, responseMessage, isNewUser);
Console.WriteLine(s);           

Or, using a verbatim string, which allows multiple lines:

var template = @"
{{
    ""response"": ""{0}"",
    ""isNewAUser"": ""{1}""
}}";

var s = string.Format(template, responseMessage, isNewUser);
Console.WriteLine(s);           

Use an interpolated string

If you're on C#6 or later, you can use an interpolated string:

var s = $@"{{ ""response"" : ""{responseMessage}"",""isNewAUser"" : ""{isNewUser}""}}";
Console.WriteLine(s);           

The double { is required in order to distinguish between placeholders and actual string literals, and the double " is required to escape the quote within the string.

John Wu
  • 50,556
  • 8
  • 44
  • 80
2

I suggested that you serialize your object, so you don't have to format your string, and also the response can be easily extended (see example below for how to serialize object). Alternatively, it's cleaner to use String.Format(), for example:

string a = String.Format("{{ {0}response{0}: {0}{1}{0}, {0}isNewAUser{0}: {0}{2}{0}}}", 
                                    '"', "This is a response Msg Eg", "false"); 

Using Serialization

    class ResponseData {
         string responseMsg {get; set; } 
         bool isNewUser {get; set; }
    }

So when you want to convert to JSON, you use Json.NET

      ResponseData response = new ResponseData(); 
      response.responseMsg = "error";
      reponse.isNewUser = false; 
      string output = JsonConvert.SerializeObject(response);

Reference: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

grepLines
  • 2,478
  • 3
  • 21
  • 32