0

我正在尝试返回一个对象的美化 json 序列化并使用 Response.Write 在 ASP.NET 中输出它,这是我的代码。不要介意不验证用户输入的明显罪行,这只是一个愚蠢的小测试应用程序。

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);
}

Formatting.Indented 似乎没有效果。我究竟做错了什么?

4

1 回答 1

4

看看你的网页的源代码,你可能会发现它的布局很好。HTML 在浏览器中呈现时会忽略/折叠空格和换行符。

您需要将json值放入<pre></pre>标签中以显示间距,或将其放入多行文本框或类似内容中。

Response.Write("<pre>");
Response.Write(json);
Response.Write("</pre>");
于 2012-12-14T09:37:23.647 回答