1

I am trying to construct JSON manually in C#.

For this I am using this string:

string Response = "{\r\n\"Token\":\"guid\"\r\n}";

But instead of this

{
 "Token" :"guid"
}

I have this

"{\r\n\"Token\":\"guid\"\r\n}"

I tried Environment.NewLine also with the same results.

What is wrong?

Update

1) As the response to the request I can send different answers: "Token" or "Request". I don't want no make this:

if(true) Result = Json(new { Token= "guid"});
else     Result = Json(new { Request = "data array"});

2) it is web api function. I need to have the "good" result on the browser site

public IHttpActionResult Logon([FromBody]Logon_Request model)
{
 //some logic
 string Response_Type=...;
 string Response_Value=...;

 string Response = "{\r\n\"" + Response_Type + "\":\"" + Response_Value + "\"\r\n}";
 IHttpActionResult Result = Ok(Response);

 return Result;
}
Michail
  • 366
  • 1
  • 3
  • 14

1 Answers1

2

I believe you are looking at the value of Response in the debugger. If you make use of your value somewhere, you will see that it is actually correct. As Alex K pointed out in the comments beneath your question, the debugger always displays strings with the escape characters, even when you use Environment.NewLine.

You can verify that it's actually, in fact, working correctly by outputting to the console (or to the immediate window, depending on your VS settings) by doing this:

System.Diagnostics.Debug.Print(Response);

One other side note: you should adhere to proper naming conventions for variables whenever possible -- e.g. response not Response -- for local variables.

rory.ap
  • 34,009
  • 10
  • 83
  • 174