Home > front end >  C# How do I remove backslashes from Json string
C# How do I remove backslashes from Json string

Time:10-06

I build the test results json attribute string, which I pass to the load test results repository method. The problem is that backslashes are appearing the TestResultRequestMessage. How do I remove the backslashes. Please see below examples:

Build the test results string that gets passed into load test results repository method

In the image above I'm showing the test results loaded to string, and that are being passed into the load test results repository method.

enter image description here

In the second image shows the test results string being loaded to the repository. Notice that the backslashes are being added. This is the problem. I need to remove the backslashes.

enter image description here

The final image shows the backslashes added to the TestResult attribute. I'm hoping this is a simple fix. Have been try to resolve the issue on my own for too long. Thanks in advance.

CodePudding user response:

Backslash in this case is an escape character for quotes. Json format requires key values to be within quotes. If we want to store json string in c# we cannot do this:

string jsonString = "{ "name" : "John" }";

for the language to store this json object as actual string we need to write

string jsonString = "{ \"name\": \"John\" }";

CodePudding user response:

Well, this is a bad idea, but maybe if you force by doing some replacements with .Replace() would help.

Now what you should do is map the json to an object instead to a string, work with it this way and then desserealize whenever you want to show it on screen.

  • Related