I have a model like this
public class Category : BaseFieldsTables
{
public ICollection<Category> Categories { get; set; }
public Category Parent { get; set; }
public int? ParentId { get; set; }
}
i want to serialize the model to json and this is my controller
var categories =
_efCategory.List().ToList().
ToList().
Select(x => new {id = x.Id, title = x.Name, children = x.Parent});
string output = JsonConvert.SerializeObject(categories, Formatting.Indented,
new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
return Json(output.Replace, JsonRequestBehavior.AllowGet);
but i get this result
"[\r\n {\r\n \"$id\": \"1\",\r\n \"id\": 1,\r\n \"title\": \"News\",\r\n \"children\": null\r\n },\r\n {\r\n \"$id\": \"2\",\r\n \"id\": 2,\r\n \"title\": \"2012\",\r\n \"children\":
{\r\n \"$id\": \"3\",\r\n \"Categories\": [\r\n {\r\n \"$id\":
This is normal. All you are seeing is the escaped version of your string (probably directly in Visual Studio).
\r\nis the equivalent of a newline character and\"is just a means to escape the". Outputted, this should appear as normal.