For various reasons, I have switched from ASP.NET MVC's built in JSON serializer (the one that returns a (see edit below)) to Newtonsoft. I didn't realize until after I began testing that the former returns a JavaScript object literal, while Newtonsoft returns a JSON formatted string.System.Web.Mvc.JsonResult
object
I like not having to parse JSON strings on the client side — having it already as an object literal is very convenient — but I want to stick with Newtonsoft for other technical reasons.
For example, instead of seeing this result on my client...
"{"Errors":["Please enter a valid email address."],"HasErrors":true}"
...I'd like to see this result:
{"Errors":["Please enter a valid email address."],"HasErrors":true} // no quotes
Is there a way to make Newtonsoft return JS object literals instead of strings?
EDIT
The way my question was framed wasn't the best. There's nothing wrong with the JsonResult
type. In fact, the solution still uses it. The only problem was the default Controller.Json
methods, which can be overridden to use Newtonsoft (Json.NET) instead of the built-in serializer.