'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 ..
Solution 1:[1]
C# 10 and lower
I prefer this; just make sure you don't have a single quote in the string.
var str = "{'Id':'123','DateOfRegistration':'2012-10-21T00:00:00+05:30','Status':0}"
.Replace("'", "\"");
C# 11 and upper
It is still in preview mode, but you can simply place your json inside a pair of triple double-quote: """
var str = """
{
"Id": "123",
"DateOfRegistration": "2012-10-21T00:00:00+05:30",
"Status": 0
}
""";
Solution 2:[2]
There is an alternate way to write these complex JSON using Expando object or XElement and then serialize.
dynamic contact = new ExpandoObject
{
Name = "Patrick Hines",
Phone = "206-555-0144",
Address = new ExpandoObject
{
Street = "123 Main St",
City = "Mercer Island",
State = "WA",
Postal = "68402"
}
};
//Serialize to get Json string using NewtonSoft.JSON
string Json = JsonConvert.SerializeObject(contact);
Solution 3:[3]
Finetuning on sudhAnsu63's answer, this is a one-liner:
With .NET Core:
string str = JsonSerializer.Serialize(
new {
Id = 2,
DateOfRegistration = "2012-10-21T00:00:00+05:30",
Status = 0
}
);
With Json.NET:
string str = JsonConvert.SerializeObject(
new {
Id = 2,
DateOfRegistration = "2012-10-21T00:00:00+05:30",
Status = 0
}
);
There is no need to instantiate a dynamic ExpandoObject.
Solution 4:[4]
You have to escape the quotes within the string like this:
String str="{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}";
Solution 5:[5]
With Verbatim String Literals (@"...") you can write inline multi-line json by swapping double quotes with pairs of double quotes - "" instead of ". Example:
string str = @"
{
""Id"": ""123"",
""DateOfRegistration"": ""2012-10-21T00:00:00+05:30"",
""Status"": 0
}";
Solution 6:[6]
you need to escape the inner quotes like so:
String str="{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}";
Solution 7:[7]
C# 11 introduces a new feature named Raw string literals. It makes working with JSON very easy. Just enclose the string using not one but three double quote characters (""") as markers:
string str = """{"Id":"123","DateOfRegistration":"2012-10-21T00:00:00+05:30","Status":0}""";
Related YouTube video by Nick Chapsas: Strings in C# 11 just got a whole lot better.
Solution 8:[8]
For an out-of-box thinking solution, I encoded the JSON to base64 so it can be imported as a string value in one line.
This preserves the line formatting without you having to write dynamic objects or escape characters manually. The format is the same as if you read the JSON from a text file:
var base64 = "eyJJZCI6IjEyMyIsIkRhdGVPZlJlZ2lzdHJhdGlvbiI6IjIwMTItMTAtMjFUMDA6MDA6MDArMDU6MzAiLCJTdGF0dXMiOjB9";
byte[] data = Convert.FromBase64String(base64);
string json = Encoding.UTF8.GetString(data);
//using the JSON text
var result = JsonConvert.DeserializeObject<object>(json);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | ice1e0 |
| Solution 3 | Pang |
| Solution 4 | KeyNone |
| Solution 5 | mrówa |
| Solution 6 | Brian |
| Solution 7 | Theodor Zoulias |
| Solution 8 |
