我有一个MVC3应用程序,它有一个详细信息页面.作为其中的一部分,我有一个描述(从数据库中检索)有空格和新行.渲染时,html会忽略新的行和空格.我想编码那些空格和新行,以便它们不被忽略.
你是怎样做的?
我尝试了HTML.Encode,但它最终显示了编码(甚至没有在空格和新行上,而是在其他一些特殊字符上)
可能重复:
使用PHP进行漂亮的JSON打印
我正在编写一个创建JSON文件的脚本.现在我只是使用json_encode(PHP 5.2.x)将数组编码为JSON输出.然后我将返回的值打印到文件并保存.问题是客户端希望能够打开这些JSON文件以便于阅读,所以我想在其中添加换行符和"漂亮打印"JSON输出.关于如何做到这一点的任何想法?我唯一能看到的另一种选择是根本不使用json_encode,只需手动编写文件内容并为每一行添加自己的换行符.
这是我得到的:
{"product_name":"prod1","val1":1,"val2":8}
Run Code Online (Sandbox Code Playgroud)
这就是我想要的:
{
"product_name":"prod1",
"val1":1,
"val2":8
}
Run Code Online (Sandbox Code Playgroud)
我想我也可以用命令后跟一个\n来替换每个逗号,对于括号也是如此...思考?
html 页面上当前的 json 视图
{"result": [{"id": "103300640", "sms_phone": "730276061", "corrected to":
"27730276061"}, {"id": "103277733", "sms_phone": "716125197", "corrected
to": "27716125197"}]}
Run Code Online (Sandbox Code Playgroud)
我希望它的格式显示如下
{
"result":[
{
"id":"103300640",
"sms_phone":"730276061",
"corrected to":"27730276061"
},
{
"id":"103277733",
"sms_phone":"716125197",
"corrected to":"27716125197"
}]
}
Run Code Online (Sandbox Code Playgroud)
JS代码:
@app.route('/<path:req_path>')
def dir_listing(req_path):
abs_path = os.path.join(UPLOAD_FOLDER, req_path)
# Check if path is a file and serve
if os.path.isfile(abs_path):
return send_file(abs_path, mimetype="application/json")
return render_template('/index.html')
Run Code Online (Sandbox Code Playgroud)
HTML 代码:
<ul>
{% for file in file_list %}
<li><a href="{{ file }}" id="list">{{ file }}</a></li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)