3

Some value is saved in JSON format in DB and if I copy it from DB it looks like this:

[{"id":"FAC2SOUTHX","name":"South District MW        ","description":"South District MW                                           ","selected":true,"required":false,"sortOrder":10}]

Now I want to write a unit test that for its mock object I need to pass that value and the mock value is a string. But C# giving error if I want to assign that value to a string variable. So I thought all I have to do is to prefix it with a "@" but didn't work either. So how can I assign that value to a string variable.

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
Bohn
  • 26,091
  • 61
  • 167
  • 254

4 Answers4

2

You have to comment all qoutes with a backslash so the string is recognized as a String e.g.

string s = "My string with \" qouted \" Values";
knechtrootrecht
  • 383
  • 2
  • 11
1

first you need to escape your string. before each inner quotes you have to use a backslash so " becomes \".

Then you could simply use something like Newtonsoft.Json to cast that string into a proper object and check the value that way.

The code is very simple you can use something like :

JsonConvert.DeserializeObject<dynamic>(your_string);
Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
1

You can also use duplicated double quotes like this

string str = @"[{""id"":""FAC2SOUTHX"",""name"":""South District MW        "",""description"":""South District MW                                           "",""selected"":true,""required"":false,""sortOrder"":10}]";

Have a look at this question Can I escape a double quote in a verbatim string literal?

Community
  • 1
  • 1
Bobby Tables
  • 2,953
  • 7
  • 29
  • 53
1

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

string someJson = "[{\"id\":\"FAC2SOUTHX\",\"name\":\"South District MW        \",\"description\":\"South District MW                                           \",\"selected\":true,\"required\":false,\"sortOrder\":10}]"
Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54