question

BrianBulla-4284 avatar image
0 Votes"
BrianBulla-4284 asked SimpleSamples published

Using Double-Double Quotes in a C# string

Hi,

In VB I use the following code to create a string with double-double quotes:

 Dim jsonString As String = "{ ""id"": """ + drawingUID + """, ""fullPath"": """ + DestPathContractDB + """, ""link"": """ + LinkPath + """, ""scanFilename"": """ + txtCard.Text + ext + """ }"


This produces something like this in my application:

 "{ ""id"": ""9999"", ""fullPath"": ""Z:\\AsBuiltDrawings\\OSHAWA\\OSH-00001.pdf"", ""link"": ""http://cctvweb/cctv/AsBuiltDrawings/OSHAWA/OSH-00001.pdf"", ""scanFilename"": ""OSH-00001.pdf"" }"


I need to convert this to C# code, but I am struggling to get the format of the double-double quotes to work. I have tried almost everything, but can't seem to get it correct. Using @, \, char.....this is driving me crazy. Here is my latest attempt:

 string jsonString = "{ " + (char)34 + (char)34 + "id" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + drawingUID + (char)34 + (char)34 + ", " + (char)34 + (char)34 + "fullPath" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + destPath + (char)34 + (char)34 + ", " + (char)34 + (char)34 + "link" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + linkPath + (char)34 + (char)34 + ", " + (char)34 + (char)34 + "scanFilename" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + cardNum + ext + (char)34 + (char)34 + " }";

Which produces this:

 "{ \"\"id\"\": \"\"9999\"\", \"\"fullPath\"\": \"\"Z:\\AsBuiltDrawings\\OSHAWA\\OSH-00001.pdf\"\", \"\"link\"\": \"\"http://cctvweb/cctv/AsBuiltDrawings/OSHAWA/OSH-00001.pdf\"\", \"\"scanFilename\"\": \"\"OSH-00001.pdf\"\" }"


Regardless of what I try I always end up getting the \"\" and not just ""

Any help is appreciated.

Thanks,


dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Are you sure that the VB code produces that? For me the following:

 Dim drawingUID As String = "drawingUID"
 Dim DestPathContractDB As String = "DestPathContractDB"
 Dim LinkPath As String = "LinkPath"
 Dim scanFilename As String = "scanFilename"
 Dim jsonString As String = "{ ""id"": """ + drawingUID + """, ""fullPath"": """ + DestPathContractDB + """, ""link"": """ + LinkPath + """, ""scanFilename"": """ + scanFilename + """ }"
 Console.WriteLine(jsonString)

Produces:

 { "id": "drawingUID", "fullPath": "DestPathContractDB", "link": "LinkPath", "scanFilename": "scanFilename" }

And that is a valid JSON string so I assume that is what the VB code is producing.

0 Votes 0 ·

Yes, I am converting some code on a tool we have been using for years. It it for sure using "". That is coming straight from the Watch window in VB and or C#.

0 Votes 0 ·

The watch window is not showing the data as it actually exists. Viorel-1 is trying to tell you that. You need to have an accurate way to see the actual data and/or learn how to interpret the watch window.

1 Vote 1 ·
BrianBulla-4284 avatar image
0 Votes"
BrianBulla-4284 answered SimpleSamples commented

For some reason I need the double-double quotes.

Going back to my original post, but writing the string to a text file I seem to be getting what I need. So with all the (char)34 + (char)34 seems to be working.

I was always using the Watch window and stopping my code when it didn't look right. Do you know why the Watch window shows it with \" instead of an actual ""??

I'll roll with this for now. Thanks for help and assistance on how the VS IDE works!! :-)

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you also write the string to file in VB in order to compare the results?

0 Votes 0 ·

Instead of accepting a post that says what has been said by others you should accept one of the posts that actually got you the result. If not then the implication is that none of the other responses helped you so for the future we know not to try to help.

1 Vote 1 ·

I will do that tomorrow.

Thanks!

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

In C# "a\"b" and @"a""b" means a"b.

To view the real text in Watch window during debugging, enter jsonString,nq.





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BrianBulla-4284 avatar image
0 Votes"
BrianBulla-4284 answered Viorel-1 edited

Thanks Viorel. I understand how to use double quotes, but the problem is in generating double-double quotes.

Do you have an example on how to get double-double quotes (ie. ""id"" into the string).

All of my samples above are taken from the "Watch" window.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


You can write like in VB:

 string jsonString = @"{ ""id"": """ + drawingUID + @""", ""filePath"": """ + DestPathContractDB + @". . .".

or use serialisation techniques.

The real text will be: { "id": "123", "fullPath":"somepath" . . .}.

0 Votes 0 ·
BrianBulla-4284 avatar image
0 Votes"
BrianBulla-4284 answered Viorel-1 edited

Hi Viorel,

I agree...it SHOULD work....but for some reason it doesn't.

So using:

string jsonString = @"{ ""id"": """ + drawingUID + @""", ""filePath"": """;

Produces this:

 "{ \"id\": \"b91541c9-4cbc-4357-998c-7228cbd9604b\", \"filePath\": \""

Any other ideas??

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


The produced text is a valid string according to C# rules. The real text does not contain these '\'. To view the text, write it to file using File.WriteAllText("myfile.txt", jsonString) and use Notepad, or simply view it in Watch window as explained before, entering jsonString,nq.



0 Votes 0 ·
BrianBulla-4284 avatar image
0 Votes"
BrianBulla-4284 answered Viorel-1 commented

Hmmm....ok, so writing to the text file I get:

{ "id": "b91541c9-4cbc-4357-998c-7228cbd9604b", "filePath": "

Using: string jsonString = @"{ ""id"": """ + drawingUID + @""", ""filePath"": """;

So the double-double quotes are still missing.

Any other ideas??

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Do you need double-double quotes or a valid JSON string?

1 Vote 1 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered SimpleSamples published

I tried posting this as a comment but this forum software would not let me.

The following C# code:

 string drawingUID = "drawingUID";
 string DestPathContractDB = "DestPathContractDB";
 string LinkPath = "LinkPath";
 string scanFilename = "scanFilename";
 string jsonString = "{ " + (char)34 + (char)34 + "id" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + drawingUID + (char)34 + (char)34 + ", " + (char)34 + (char)34 + "fullPath" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + DestPathContractDB + (char)34 + (char)34 + ", " + (char)34 + (char)34 + "link" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + LinkPath + (char)34 + (char)34 + ", " + (char)34 + (char)34 + "scanFilename" + (char)34 + (char)34 + ": " + (char)34 + (char)34 + scanFilename + (char)34 + (char)34 + " }";
 Console.WriteLine(jsonString);

Produces:

 { ""id"": ""drawingUID"", ""fullPath"": ""DestPathContractDB"", ""link"": ""LinkPath"", ""scanFilename"": ""scanFilename"" }

And that is what you say you need, correct?

I do not understand why you need the double double-quotes.

If it were me I would do something like:

 jsonString = "{ " + FormatJSON("id", "drawingUID") + ", " + FormatJSON("fullPath", "DestPathContractDB") + ", " + FormatJSON("link", "LinkPath") + ", " + FormatJSON("scanFilename", "scanFilename") + " }";

Using:

 static string FormatJSON(string name, string value)
 {
     return "\"\"" + name + "\"\": " + "\"\"" + value + "\"\"";
 }

Alternatively it seems to me that you could use a real JSON library such as Newtonsoft to generate valid JSON then just process that and change each double-quote to double double-quotes. You can generate valid JSON as simply as:

 Drawing d = new Drawing();
 d.id = drawingUID;
 d.fullPath = DestPathContractDB;
 d.link = LinkPath;
 d.scanFilename = LinkPath;
 jsonString = JsonConvert.SerializeObject(d);

Using a class such as:

 public class Drawing
 {
     public string id { get; set; }
     public string fullPath { get; set; }
     public string link { get; set; }
     public string scanFilename { get; set; }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BrianBulla-4284 avatar image
0 Votes"
BrianBulla-4284 answered SimpleSamples commented

Well, in VB the watch window was giving me double-double quotes, so that is what I was going for with my C# code, but maybe I really only need the single.

Especially since with the confirmed double-double quotes nothing is working, I think I will have to go for the single double quotes and try this again.

Thanks,

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes you probably just need the single double-quotes and can use a real JSON library such as Newtonsoft to generate valid JSON.

0 Votes 0 ·