Rectangle 27 0

c How to turn plain text json data into string?


Part of the issue looks to be the use of double quotes which can be escaped with a backslash \, however to have a multi-line string in C#, you also need to append an @ symbol at the start like shown in this answer https://stackoverflow.com/a/1100265/2603735

Note
Rectangle 27 0

c How to turn plain text json data into string?


string testjson = @"
{
DataA: string,
DataB: datetime,
DataC: {
    DataC1: datetime,
    DataC2: datetime,
    DataC3: datetime,
    DataC4: int,
    DataC5: int,
    DataC6: string,
    DataC7: int,
    DataC8: object
},
DataD: {
    DataD1: decimal,
    DataD2: decimal,
    DataD3: string,
    DataD4: int,
    DataD5: decimal,
    DataD6: string,
    DataD7: {
        DataD7i: null,
        DataD7ii: [] 
    }
}
}"
Note
Rectangle 27 0

c How to turn plain text json data into string?


  • Make that text file an embedded resource, which you can load via Assembly.GetExecutingAssembly().GetManifestResourceStream(...).
  • Or set the file's project property "Copy to output directory" to "If newer". Then just read it via File.ReadAllText.

In my unit test projects, whenever I have "mass" text, I put that content into a separate text file. Then you have two choices:

Keeping it in a separate file makes editing/maintenance a lot easier.

Note