Json.NET adding backslash while returning json serialized string

No. it doesn’t

class Program
{
    class Book
    {
        public int ID;
        public string BookName;
    }

    static void Main()
    {
        var books = new List<Book> { new Book { ID = 1, BookName = "A" }, new Book { ID = 2, BookName = "B" } };

        var x = from d in books
        select new
        {
            ID = d.ID,
            BookName = d.BookName
        };

        string str = JsonConvert.SerializeObject(x.ToList());
        Console.WriteLine(str);
    }
}

There could be two problems:

A) You are looking at the result from the debugger. To check for this, Put the JsonConvert in a temporary variable (like I did) and look at it with the debugger. Click on the arrow right of the hourglass and select Text Visualizer.

or

B) The calling method is transforming the object again to Json, so escaping everything.

Leave a Comment