Store Hardcoded JSON string to variable

后端 未结 4 959
情书的邮戳
情书的邮戳 2021-02-05 01:48

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         


        
相关标签:
4条回答
  • 2021-02-05 02:27

    Writing JSON inline with c# in strings is a bit clunky because of the double quotes required by the JSON standard which need escaping in c# as shown in the other answers. One elegant workaround is to use c# dynamic and JObject from JSON.Net.

    dynamic message = new JObject();
    message.ErrorMessage = "";
    message.ErrorDetails = new JObject();
    message.ErrorDetails.ErrorId = 111;
    message.ErrorDetails.Description = new JObject();
    message.ErrorDetails.Description.Short = 0;
    
    Console.WriteLine(message.ToString());
    
    // Ouputs:
    // { 
    //   "ErrorMessage": "",
    //   "ErrorDetails": {
    //     "ErrorID": 111,
    //     "Description": {
    //       "Short": 0
    // .....  
    

    See https://www.newtonsoft.com/json/help/html/CreateJsonDynamic.htm.

    0 讨论(0)
  • 2021-02-05 02:32

    First things first, I'll throw this out there: It's for this reason in JSON blobs that I like to use single quotes.

    But, much depends on how you're going to declare your string variable.

    string jsonBlob = @"{ 'Foo': 'Bar' }";
    string otherBlob = @"{ ""Foo"": ""Bar"" }";
    

    ...This is an ASCII-encoded string, and it should play nicely with single quotes. You can use the double-double-quote escape sequence to escape the doubles, but a single quote setup is cleaner. Note that \" won't work in this case.

    string jsonBlob = "{ 'Foo': 'Bar' }";
    string otherBlob = "{ \"Foo\": \"Bar\" }";
    

    ...This declaration uses C#'s default string encoding, Unicode. Note that you have to use the slash escape sequence with double quotes - double-doubles will not work - but that singles are unaffected.

    From this, you can see that single-quote JSON literals are unaffected by the C# string encoding that is being used. This is why I say that single-quotes are better to use in a hardcoded JSON blob than doubles - they're less work, and more readable.

    0 讨论(0)
  • 2021-02-05 02:46

    Simple Approach is to copy the JSON to a .json file and read that file in the code

    string jsonData = string.Empty;
    jsonData = File.ReadAllText(@"\UISettings.json");
    
    0 讨论(0)
  • 2021-02-05 02:54

    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"": """"}}";
    
    0 讨论(0)
提交回复
热议问题