user1811801 user1811801 -4 years ago 137
C# Question

How to write JSON string value in code?

I want to store the following string in a String variable


{"Id":"123","DateOfRegistration":"2012-10-21T00:00:00+05:30","Status":0}


This is the code I use ..

String str="{"Id":"123","DateOfRegistration":"2012-10-21T00:00:00+05:30","Status":0}";


.. but it's showing error ..

Answer Source

You have to do this

String str="{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}";


Please see this for reference
Also from msdn :)

Short Notation  UTF-16 character    Description
\'  \u0027  allow to enter a ' in a character literal, e.g. '\''
\"  \u0022  allow to enter a " in a string literal, e.g. "this is the double quote (\") character"
\\  \u005c  allow to enter a \ character in a character or string literal, e.g. '\\' or "this is the backslash (\\) character"
\0  \u0000  allow to enter the character with code 0
\a  \u0007  alarm (usually the HW beep)
\b  \u0008  back-space
\f  \u000c  form-feed (next page)
\n  \u000a  line-feed (next line)
\r  \u000d  carriage-return (move to the beginning of the line)
\t  \u0009  (horizontal-) tab
\v  \u000b  vertical-tab
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download