How to turn plain json text data into string?

I am trying to mock some test data to verify that the json string is deserialized into the object correctly.

I have some json data with a length of 660 rows, so I only included a part

{ "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": [ 

I created the appropriate classes, but I'm currently trying to test them. However, I cannot get this json data into a string, since double quotes close the string. I also tried using ecsapes, but to no avail.

 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": [" 

I want to call

  ObjectA objectblah= JsonConvert.DeserializeObject<ObjectA>(output); 

But cannot get json to string. I know this is a trivial problem, but I am new and stuck on this issue. Any help would be greatly appreciated.

thanks

+6
source share
3 answers

Part of the problem is the use of double quotes, which can be escaped using the backslash \ , however, in order to have a multi-line string in C #, you also need to add the @ character at the beginning as shown in this answer fooobar.com/questions/12878 / ...

+4
source

In my unit test projects, whenever I have bulk text, I put this content in a separate text file. Then you have two options:

  • Make this text file an embedded resource that can be downloaded via Assembly.GetExecutingAssembly (). GetManifestResourceStream (...).
  • Or, set the project file property "Copy to output directory" to "If newer." Then just read it through File.ReadAllText.

Saving in a separate file simplifies editing / maintenance.

+3
source

Use it as follows:

  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: [] } } }" 
+3
source

Source: https://habr.com/ru/post/979673/


All Articles