Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a string as follows:
C#
[{"Id":"908f08bc-024e-4371-9f1b-a15156f9fe85","It":false,"Ch":[]}]

I wanted to store it in a string as follows:
C#
String lstrtest=
[{"Id":"908f08bc-024e-4371-9f1b-a15156f9fe85","It":false,"Ch":[]{]

How can I do it?

What I have tried:

I tried appending double quotes as follows, but it didn't work.
C#
String  lobj = "[{\"Id\":\"908F08bc-024E-4371-9F1b-a15156f9fe85\",\"It\":false,\"Ch\":[]}]"
Posted
Updated 17-Aug-23 6:01am
v6
Comments
Richard Deeming 17-Aug-23 4:09am    
Your title says C#; your tag says C#; but your code is in VB.NET. So which is it?
Vert12 17-Aug-23 4:25am    
sorry its a typo mistake, it in C# only, modified question

Hi Before pass json value to string, u need to replace single double quotes to 2x times double quotes, like this if u write code in vb
Dim aa As String = "[{""Id"":""908F08bc-024E-4371-9F1b-a15156f9fe85"",""It"":False,""ch"":""kkk""}]"


Best Regards
Aravind
 
Share this answer
 
v3
In C#, you can do it in two ways:
1) Prefix the string with an atsign, which disables the escape character "\" and then double up each double quote:
C#
string s = @"[{""Id"":""908F08bc-024E-4371-9F1b-a15156f9fe85"",""It"":false,""Ch"":[]}]";

Or
2) Use the escape character before each double quote:
C#
string s = "[{\"Id\":\"908F08bc-024E-4371-9F1b-a15156f9fe85\",\"It\":false,\"Ch\":[]}]";

But the code you show doesn't match the tags: that is VB code, which doesn't use a backslash escape code - so you have to double up double quotes:
VB
Dim s As String = "[{""Id"":""908F08bc-024E-4371-9F1b-a15156f9fe85"",""It"":false,""Ch"":[]}]"
Decide which language you are using and always tag it appropriately: different languages need different solutions!


Quote:
tại sao chúng tôi không nhìn thấy nó rõ hơn
(Why can't we see it more clearly?)


Because the string has to have delimiters: the double quotes indicate to the system where it starts and stops. So if you don't either have an escape character or double the delimiter up when you want one inside the string it assumes the first double quote it meets is the end of the string - it can't "read your mind" and work out what you intended to do!

The alternative is as Richard said: use Raw String Literals (if you are using .NET 7 & C# 11.0 or above). In that case you can write this:
C#
string s = """
           [
             {
             "Id":"908f08bc-024e-4371-9f1b-a15156f9fe85",
             "It":false,
             "Ch":[]
             }
           ]
           """;
Which is even more readable than the single line version.
 
Share this answer
 
v3
Comments
Graeme_Grant 17-Aug-23 1:54am    
for #1, you mean string literals ;)
Richard Deeming 17-Aug-23 4:01am    
Three ways - don't forget raw string literals[^]! :)
string s = """[{"Id":"908f08bc-024e-4371-9f1b-a15156f9fe85","It":false,"Ch":[]}]""";

You can also use interpolation without having to escape the braces:
string s = $$"""[{"Id":"{{Guid.NewGuid():N}}","It":false,"Ch":[]}]""";
OriginalGriff 17-Aug-23 5:16am    
I had never heard of those: I can see why from the example on the page - I may start using them.
[no name] 17-Aug-23 8:50am    
tại sao chúng tôi không nhìn thấy nó rõ hơn
OriginalGriff 17-Aug-23 9:07am    
Answer updated

There is a simple solution to this problem that was introduced in C#11 ( see the comment from Graeme_Grant for further details). Use two string interpolation characters $ to escape the formatting characters { and }, followed by three double quotation marks used to escape the quotation marks that are in the text. Then place your text on a new line followed by another newline containing three double quotation marks and a semi colon.


C#
string test = $$"""
[{"Id":"908f08bc-024e-4371-9f1b-a15156f9fe85","It":false,"Ch":[]}]
""";
Console.WriteLine(test);
 
Share this answer
 
v3
Comments
Graeme_Grant 17-Aug-23 3:45am    
George Swan 17-Aug-23 4:03am    
Thanks Graeme, you are quite correct. I will update my answer.
Richard Deeming 17-Aug-23 4:05am    
It's supported perfectly well in .NET Framework - I'm using it in several MVC5 projects. You just need to edit the project file to set <LangVersion>11.0</LangVersion>, since Visual Studio won't let you do that through the UI.

C# language versions 8+ aren't officially supported in .NET Framwork, but a lot of stuff does work: see this comment for C# 9[^], and this follow-up for C# 10[^]. I haven't found an equivalent one for C# 11, but it's easy enough to test. :)

NB: Some features require additional attributes or classes to be defined, but libraries like PolySharp[^] can automatically generate those for you.
Graeme_Grant 17-Aug-23 4:08am    
I stand corrected. I have found most new lang features are not supported on both.

My reference is: What's new in C# 11 - C# Guide | Microsoft Learn[^] and C# language versioning - C# Guide | Microsoft Learn[^]

Quote: "C# 11 is supported on .NET 7. For more information, see C# language versioning."

So, technically, I am correct when the 3rd party lib PolySharp is not used. ;)
Richard Deeming 17-Aug-23 4:29am    
Raw string literals don't require any additional classes or attributes, so they don't need PolySharp. :)

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



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