Search code examples
c#json

Store Hardcoded JSON string to variable


I'm having an issue storing this json string to a variable. It's gotta be something stupid I am missing here

private string someJson = @"{
    "ErrorMessage": "",
    "ErrorDetails": {
        "ErrorID": 111,
        "Description": {
            "Short": 0,
            "Verbose": 20
        },
        "ErrorDate": ""
    }
}";

Solution

  • You have to escape the "'s if you use the @ symbol it doesn't allow the \ to be used as an escape after the first ". So the two options are:

    don't use the @ and use \ to escape the "

    string someJson = "{\"ErrorMessage\": \"\",\"ErrorDetails\": {\"ErrorID\": 111,\"Description\":{\"Short\": 0,\"Verbose\": 20},\"ErrorDate\": \"\"}}";
    

    or use double quotes

    string someJson =@"{""ErrorMessage"": """",""ErrorDetails"": {""ErrorID"": 111,""Description"": {""Short"": 0,""Verbose"": 20},""ErrorDate"": """"}}";