Mar*_*tim 0 c# asp.net json json.net
我试图返回一个对象的美化的 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);
}
Run Code Online (Sandbox Code Playgroud)
看来 Formatting.Indented 没有效果。我究竟做错了什么?
查看一下您网页的源代码,您可能会发现它的布局都很好。HTML 在浏览器中呈现时会忽略/折叠空格和换行符。
您需要将json值放入<pre></pre>标签中以显示间距,或者将其放入多行文本框或类似的内容中。
Response.Write("<pre>");
Response.Write(json);
Response.Write("</pre>");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
865 次 |
| 最近记录: |