Amit Hasan
Amit Hasan

Reputation: 1450

How to get indented JSON output in asp.net MVC 5 to view in browser?

This is only for debugging purpose. Only returning JSON, outputs unreadable JSON string on webpage. Is there any quick solution to view formatted json on webpage?

Looking for a C# function that will take json object and return formatted output as string from my action method.

Upvotes: 0

Views: 1090

Answers (2)

Kaspars Ozols
Kaspars Ozols

Reputation: 7017

Just change JSON global settings in your Global.asax file.

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
{
    Formatting = Formatting.Indented
};

It should produce indented JSON for all your Web API endpoints.

Then you can inspect resulting JSON directly in browser or using some HTTP traffic capturing tool like Fiddler.

Upvotes: 2

Racil Hilan
Racil Hilan

Reputation: 25361

Use the JavaScript JSON.Stringify() function. Example:

$.ajax({
  method: "POST",
  url: "/yourController/yourAction",
  data: { name: "John", location: "Boston" }
}).done(function( data ) {
    alert(JSON.Stringify(data));
});

Upvotes: 1

Related Questions