Get Prettified JSON from MVC 3 JsonResult

Context
Language: C #
Platform Version: Microsoft.Net Framework 4.0
Operating System: Windows 7 Professional (64-bit)
Limitations: Microsoft MVC.Net 3.0

Problem
These days, I look a lot at JSON in the browser, pointing the browser to one Controller action or another on my local dev server and visually parsing JSON to make sure everything is formatted the way we want. The JSON returned by the MVC 3 serializer (or the JSON.Net serialization follower) always returns a thumbnail, so I end up with something like this:

{"Bars":[{"Name":"Ghost Bar","Address":"2440 Victory Park Lane, 33rd Floor, Dallas, TX 75219","OpenDate":"\/Date(1208062800000)\/","Status":"Open"},{"Name":"M-Street Bar","Address":"5628 Sears Street, Dallas, TX 75206","OpenDate":"\/Date(1064811600000)\/","Status":"Closed"},{"Name":"Zephyr\u0027s Lounge","Address":"3520 Greenville Avenue, Dallas, TX 75206","OpenDate":"\/Date(981007200000)\/","Status":"Open"}]} 

Question
I would really like to find a way, at least during debugging, to make JsonResult be β€œfaithful” so it looks like this:

 { "Bars": [ { "Name": "Ghost Bar", "Address": "2440 Victory Park Lane, 33rd Floor, Dallas, TX 75219", "OpenDate": "\/Date(1208062800000)\/", "Status": "Open" }, { "Name": "M-Street Bar", "Address": "5628 Sears Street, Dallas, TX 75206", "OpenDate": "\/Date(1064811600000)\/", "Status": "Closed" }, { "Name": "Zephyr\u0027s Lounge", "Address": "3520 Greenville Avenue, Dallas, TX 75206", "OpenDate": "\/Date(981007200000)\/", "Status": "Open" } ] } 

I know several online tools that will allow you to embed and format JSON. This is an additional step, and I have a lot of time. I would prefer a software solution. I would also like to enable / disable it through the configuration or the #if compiler directive.

I already did a quick search about this and find this post in a stack overflow . However, the above code example is rather incomplete. There are also a few links, but they seem to be dead.

In any case, I would like to find a way to get the β€œfaithful” JSON from ActionResult. Any help related to the ad.

The following source code will reproduce the initial line, not intended for JSON, which I gave as an example. Feel free to copy / paste / edit.

 using System.Collections.Generic; using System.Text; using System.Web.Mvc; namespace PrettyJsonResult.Controllers { public class DefaultController : Controller { public JsonResult Index() { var foo = new Foo(); foo.Bars.Add(new Bar { Address = "2440 Victory Park Lane, 33rd Floor, Dallas, TX 75219", Name = "Ghost Bar", Status = "Open" }); foo.Bars.Add(new Bar { Address = "5628 Sears Street, Dallas, TX 75206", Name = "M-Street Bar", Status = "Closed" }); foo.Bars.Add(new Bar { Address = "3520 Greenville Avenue, Dallas, TX 75206", Name = "Zephyr Lounge", Status = "Open" }); return Json(foo, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet); } } public class Foo { public Foo() { Bars = new List<Bar>(); } public List<Bar> Bars { get; set; } } public class Bar { public string Name { get; set; } public string Address { get; set; } public string Status { get; set; } } } 
+6
source share
2 answers

There is not much answer, but if you switch the Json serializer from the built-in (JavaScriptSerializer) to Json.NET (which has a bunch of advantages outside of this problem), you can do something like this:

 JsonConvert.SerializeObject( myObjectDestinedForJSON, Formatting.Indented); 

Settings documents here: http://james.newtonking.com/projects/json/help/

+15
source

Pragmatic and useful for all situations:

Use chrome + this extension: https://chrome.google.com/webstore/detail/chklaanhfefbnpoihckbnefhakgolnmc

it will be pretty JSON format when it realizes that it is json.

+4
source

Source: https://habr.com/ru/post/911817/


All Articles