JSON格式化程序库 [英] JSON formatter lib

查看:211
本文介绍了JSON格式化程序库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种格式化方法(如空格,适当的换行符)JSON结果,以便我可以显示实际结果但格式正确。

I'm looking for a way to format (as in whitespace, newlines where suitable) a JSON result so that I can display the actual result but well formatted.

$.ajax({
                url: "/Home/Send",
                type: "POST",
                data: JSON.stringify(request),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    $("#ResponseBody").val(data.ResponseBody);
                },
                error: function (data) {
                    alert(data);
                }
            });

这是我的代码,工作正常 data.ResponseBody 包含JSON,但正如预期的那样,格式不正确。

this is my code, which works fine data.ResponseBody contains the JSON, but as expected, it is not well formatted.

有没有人知道允许我格式化响应的jQuery插件/方法?

Does anyone know of a jQuery plugin / method that would allow me to format the response?

推荐答案

你可以简单地使用 JSON.stringify

You can simply use the third parameter of JSON.stringify:

    success: function (data) {
        var obj = JSON.parse(data.ResponseBody);
        $("#ResponseBody").val(JSON.stringify(obj, null, 4));
    },

不要忘记添加CSS规则,例如 #ResponseBody {white-space:pre;} 显示换行符。

Don't forget to add a CSS rule like #ResponseBody {white-space: pre;} to make newlines display.

这篇关于JSON格式化程序库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆