When you are debugging a program, then type s,nq
into Watch window or Immediate window, where s is your variable.
The \"
is required to specify a "
inside the string; the \
is not actually present is the string.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
how to remove the \
in string?
there is string:
{\"id\":1,\"value\":\"max\"}
how to remove the \ but keep the "
?
{"id":1,"value":"max"}
and also how to get the System.Text.Json.JsonSerializer.Serialize get the string without \ ?
When you are debugging a program, then type s,nq
into Watch window or Immediate window, where s is your variable.
The \"
is required to specify a "
inside the string; the \
is not actually present is the string.
Hi,
{\"id\":1,\"value\":\"max\"}
is the minimal string expression of {"id":1,"value":"max"}
. So you couldn't remove .
System.Text.Json.JsonSerializer.Serialize
is used to convert object to a json string. But {\"id\":1,\"value\":\"max\"}
is already a json string format, so there's no point to convert.
Instead, {\"id\":1,\"value\":\"max\"}
can be used to deserialized(convert from json string to object). JsonConvert
(from package Newtonsoft.Json)is better for use because it doesn't need class information when deserialize.
Another thing you may want to do is convert a jsonString to jsonObject, it will be easier to read values.
You can try the following code.
string a = "{\"id\":1,\"value\":\"max\"}";
var b = JsonConvert.SerializeObject(a);
var c = JsonConvert.DeserializeObject(a);
var d = JsonObject.Parse(a);
Console.WriteLine("orginial string:" + a);
Console.WriteLine("Serialize:"+ b);
Console.WriteLine("Deserialize:" + c);
Console.WriteLine("value:"+d["value"]);
Output
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.