Home > database >  How to enter a multiline string representing JSON into Visual Studio
How to enter a multiline string representing JSON into Visual Studio

Time:08-20

If I enter this into Visual Studio editor, it is a mess and it doesn't work as s is spread over multiple lines in the Editor and contains embedded " characters.

// This is my json string
string s = "
{
    "label": "MyLabel",
    "values": {
      "Key1": "Value1",
      "Key2": "Value2",
      "Key2": "Value3",
    }
}";

How do I annotate this with @ and \ so that is is a edit-time JSON string ?

CodePudding user response:

Use a verbatim string using @"" and replace " with ""

Like this:

// This is my json string
string s = @"
{
    ""label"": ""MyLabel"",
        ""values"": {
        ""Key1"": ""Value1"",
        ""Key2"": ""Value2"",
        ""Key2"": ""Value3"",
    }
}
            ";
  • Related