I’m trying to return a beautified json serialization of an object and output it in ASP.NET using Response.Write, this is my code. Nevermind the obvious crime in not validating user input, this is just a silly little test app.
try
{
var customer = this.sc.GetCustomer(Convert.ToInt32(TextBox1.Text));
var json = JsonConvert.SerializeObject(customer, Formatting.Indented);
Response.Write(json);
}
catch (Exception ex)
{
Response.Write("An error occurred: " + ex.Message);
}
It appears that Formatting.Indented has no effect. What am I doing wrong?
Have a look at your web page’s source and you’ll probably see it all nicely laid out. HTML ignores/collapses spaces and newlines when it’s rendered in the browser.
You’ll need to put your
jsonvalue in<pre></pre>tags to get the spacing to show up, or put it inside a multiline text box, or something similar.