0

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 ?

Ivan
  • 7,448
  • 14
  • 69
  • 134
  • By the way this has nothing to do with JSON. Think about it. How would you parse `string s = " { "label": "MyLabel" };`. Literal strings start and end with a double quote, so what that looks like to the compiler is `string s = " { "` followed by and unexpected (and unquoted) `label` followed by an unexpected literal string (`": "`) followed by an unexpected and unquoted `MyLabel` followed by a dangling start of a string (with no closing quote): (`" };`). It's unparsable – Flydog57 Aug 19 '22 at 20:18

1 Answers1

1

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"",
    }
}
            ";
Lzh
  • 3,585
  • 1
  • 22
  • 36