1

I have a set of codes where i have done error handling. I want the result to be {"ErrorMessage": Server is down} However i keep getting the result as {"ErrorMessage":"Server is down"}.

Here are my codes:

catch (Exception e) { var result = "Server is down"; return Ok(new { ErrorMessage = result }); } 

I have tried using '@' and '\' characters however it did not work. How do i escape those extra double quotes? Someone please do help me. Thank you so much in advance.

10
  • 4
    {"ErrorMessage": Server is down} is invalid JSON. See json.org for confirmation: A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. Are you absolutely, positively sure you want that? Commented Dec 19, 2017 at 5:10
  • Please let me know the right way to do this would greatly appreciate Commented Dec 19, 2017 at 5:12
  • 2
    {"ErrorMessage":"Server is down"} is already the right way. That's a valid JSON object with a single property named ErrorMessage whose value is the string Server is down. Or by Please let me know the right way to do this do you mean, "Please tell me how to generate this invalid JSON anyway, I am absolutely positively sure I need it." Commented Dec 19, 2017 at 5:13
  • 1
    I thought both the ways were incorrect so i was looking for another way to do it. Now i know {"ErrorMessage":"Server is down"} is correct so thanks :) Commented Dec 19, 2017 at 5:19
  • 1
    For this however, the output is a string therefore it is right to have that double quotes and that is the valid JSON way. -- yes, this is correct. The json standard is here: json.org and shows that a JSON primitive value is either a quoted string, a number, true, false or null. Commented Dec 19, 2017 at 5:35

1 Answer 1

1

I have decided to leave the result as it is as {"ErrorMessage":"Server is down"} is the correct way of displaying a JSON output. As explained by @dbc, that is a valid JSON object with a property named ErrorMessage whose value is the string Server is down.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.