Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is a Json string
When I pastes it (the 1st and last characters, namely the double quote, are removed) in
JSON Formatter & Validator[^]
for validation.
JsonString = "{\"ownerSysId\":\"MyIMG\",\"applicationId\":31000,\"uploadFiles\":[{\"id\":2121,\"path\":\"\\\\jServer.local\\gisprod\\Photos\\engineering\\A01\\A01\\PF(2)\\2019\",\"fileName\":\"\\E029089_02-21-2019_085353.png\",\"bearing\":\"none\",\"createdDate\":\"2019-01-03T08:59\"},{\"id\":2141,\"path\":\"\\\\jServer.local\\gisprod\\Photos\\engineering\\B02\\B02\\SHFT(VB4)\\2017\",\"fileName\":\"\\C020533_02-28-2019_131017.png\",\"bearing\":\"none\",\"createdDate\":\"2017-04-27T12:37\"}]}";

The errors received are
Strings should be wrapped in double quotes.

Using NotePad to remove all of the single back-slash, the text likes that below:
{"ownerSysId":"MyIMG","applicationId":31000,"uploadFiles":[{"id":2121,"path":"\\\\jServer.local\\gisprod\\Photos\\engineering\\A01\\A01\\PF(2)\\2019","fileName":"\\E029089_02-21-2019_085353.png","bearing":"none","createdDate":"2019-01-03T08:59"},{"id":2141,"path":"\\\\jServer.local\\gisprod\\Photos\\engineering\\B02\\B02\\SHFT(VB4)\\2017","fileName":"\\C020533_02-28-2019_131017.png","bearing":"none","createdDate":"2017-04-27T12:37\"}]}

it gets validated.
I have tried multiple string function to remove single back-slash, but not effective. I will appreciate if you can help for this issue.

What I have tried:

Tried
JsonValue.Replace("\\","")
but not working
Tried
JsonValue.TrimStart('\\').TrimEnd('\\')
but not working
Posted
Updated 24-May-19 8:36am
v2

Your final string misses a closing
\"

\"2017-04-27T12:37}]}";
 
Share this answer
 
Comments
s yu 24-May-19 10:01am    
You are right. But added '\"' after \"2017-04-27T12:37 as \"2017-04-27T12:37\"
Still got the error: Strings should be wrapped in double quotes.
Those backslashes don't exist in your string. They only exist in your source code.

Depending on your settings, you might also see them in the debugger.

But if you output the string to the console, you'll see that the string contains perfectly valid JSON:
C# String Escape | C# Online Compiler | .NET Fiddle[^]
C#
string JsonString = "{\"ownerSysId\":\"MyIMG\",...
Console.WriteLine(JsonString);
Output:
{"ownerSysId":"MyIMG",...
 
Share this answer
 
Because " and \ have special meaning in C/C++/C# languages, they are escaped when they appear in source code.
Every programming language use this technique to include special chars in a string in source code.
So the json string you see in source code is not what is in executable after compilation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900