Newtonsoft.Json SerializeObject without escape backslashes

What you see in debugger when looking at the json value is the string value that you should use in a C# file to obtain the same value.

Indeed you could replace

dynamic foo = new ExpandoObject();
foo.Bar = "something";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);

with

string json = "{\"Bar\":\"something\"}";

without changing the program's behaviour.

Thus, to obtain a different value, you should change how JsonConvert works, but JsonConvert conforms to the JSON standard, thus forget it!

If you are not actually serializing ExpandoObject (nor any other sealed class out of your control), you can use the DebuggerDisplayAttribute on the types that you are serializing in json, to define how the object will be shown during debug (in your code, the foo instance).

But a string is a string and VisualStudio is right: double-quotes must be escaped.

If this happens to you while returning the value from a WebApi method, try returning the object itself, instead of serializing the object and returning the json string. WebApi will serialize objects to json in the response by default; if you return a string, it will escape any double quotes it finds.

So instead of:

public string Get()
{
    ExpandoObject foo = new ExpandoObject();
    foo.Bar = "something";
    string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);
    return json;
}

Try:

public ExpandoObject Get()
{
    ExpandoObject foo = new ExpandoObject();
    foo.Bar = "something";
    return foo;
}

Old question but I found this,

In my case, I was looking at the JSON string in a debugger and I found that was adding the escaping.

And when I printed JSON to console, it was without escape characters. Hope it helps.

Its Just simple make the return IHttpActionResult and return the object

  public IHttpActionResult Get()
    {

        ExpandoObject foo = new ExpandoObject();
        foo = //query result

        return ok(foo)
    }

Instead of using Newstonsoft.Json you should employ the JavaScriptSerializer.Serialize Method:

dynamic foo = new ExpandoObject();
foo.Bar = "something";
var js = new JavaScriptSerializer( );
string json = js.Serialize(foo);

This method produces exactly the output you are looking for. I read about it here.

Hey I Just simply write out put to a file

 using (System.IO.StreamWriter file = 
            new System.IO.StreamWriter(@"jsonGonna.txt", true))
        {
            file.WriteLine(json);
        }

now just run the program and you will get without black slash and it good for big programs where you need to save JSON multiple times

    [HttpGet]
    public object Get(int id)
    {

        object result = "";
        var db = new dbEntities();
        var EO = new System.Dynamic.ExpandoObject() as IDictionary<string, Object>; //needed to return proper JSON without escape slashes
        try
        {

            IEnumerable<usp_GetComplaint_Result> aRow =  db.usp_GetComplaint(id);

            string DBL_QUOTE = new string(new char[] { '"' });
            result = "{";

            foreach (usp_GetComplaint_Result oneRow in aRow)
            {
                System.Reflection.PropertyInfo[] properties = typeof(usp_GetComplaint_Result).GetProperties();

                foreach(System.Reflection.PropertyInfo property in properties)
                {
                    var vValue = property.GetValue(oneRow) == null ? "null" : property.GetValue(oneRow);
                    EO.Add(property.Name,vValue);
                }
                break;
            }

        }
        catch (Exception ex)
        {
            result = ex.Message;
            EO.Add("Error", result);
        }
        finally
        {
            db.Dispose();
        }

        return Ok(EO);

    }