4

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.

2
  • Encolse that JSON in double quotes and escape all the double quotes inside it. Commented Jul 14, 2016 at 13:50
  • Or just replace double quotes inside the JSON with single quotes, whichever is easier for you. Commented Jul 14, 2016 at 13:51

4 Answers 4

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";
Sign up to request clarification or add additional context in comments.

Comments

2

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?

Comments

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);

Comments

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}]"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.