从MVC 3 JsonResult获得Prettified JSON

时间:2012-03-27 22:52:24

标签: c# json asp.net-mvc jsonresult

上下文
语言:C#
平台版本:Microsoft .Net Framework 4.0
操作系统:Windows 7 Professional(64位)
约束:Microsoft MVC.Net 3.0

问题
我发现自己现在在浏览器中大量查看JSON,将浏览器指向一个Controller Action或我本地开发服务器上的另一个,并在视觉上解析JSON以确保所有内容都按照我们想要的方式进行格式化。 MVC 3序列化程序(或JSON.Net序列化程序)返回的JSON总是返回一个缩小的字符串,所以我结束了这样的事情:

{"Bars":[{"Name":"Ghost Bar","Address":"2440 Victory Park Lane, 33rd Floor, Dallas, TX 75219","OpenDate":"\/Date(1208062800000)\/","Status":"Open"},{"Name":"M-Street Bar","Address":"5628 Sears Street, Dallas, TX 75206","OpenDate":"\/Date(1064811600000)\/","Status":"Closed"},{"Name":"Zephyr\u0027s Lounge","Address":"3520 Greenville Avenue, Dallas, TX 75206","OpenDate":"\/Date(981007200000)\/","Status":"Open"}]}

问题
我真的想找到一种方法,至少在调试过程中,让JsonResult被“美化”,使它看起来更像这样:

{
  "Bars": [
    {
      "Name": "Ghost Bar",
      "Address": "2440 Victory Park Lane, 33rd Floor, Dallas, TX 75219",
      "OpenDate": "\/Date(1208062800000)\/",
      "Status": "Open"
    },
    {
      "Name": "M-Street Bar",
      "Address": "5628 Sears Street, Dallas, TX 75206",
      "OpenDate": "\/Date(1064811600000)\/",
      "Status": "Closed"
    },
    {
      "Name": "Zephyr\u0027s Lounge",
      "Address": "3520 Greenville Avenue, Dallas, TX 75206",
      "OpenDate": "\/Date(981007200000)\/",
      "Status": "Open"
    }
  ]
}

我知道several online tools可以粘贴和格式化JSON。这是一个额外的步骤,并花了我很多时间。我更喜欢程序化解决方案。我也希望能够通过配置或#if编译器指令打开/关闭它。

我已经对此进行了粗略搜索并遇到了this post on Stack Overflow。但是,给出的代码示例非常不完整。还提供了几个链接,但它们似乎已经死了。

无论如何,我想找到一种从ActionResult中获取“美化”JSON的方法。任何有用的帮助。

以下源代码将重现我给出的初始未经修饰的JSON字符串作为示例。随意复制/粘贴/编辑。

using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;

namespace PrettyJsonResult.Controllers
{
    public class DefaultController : Controller
    {
        public JsonResult Index()
        {
            var foo = new Foo();
            foo.Bars.Add(new Bar { Address = "2440 Victory Park Lane, 33rd Floor, Dallas, TX 75219", Name = "Ghost Bar", Status = "Open" });
            foo.Bars.Add(new Bar { Address = "5628 Sears Street, Dallas, TX 75206", Name = "M-Street Bar", Status = "Closed" });
            foo.Bars.Add(new Bar { Address = "3520 Greenville Avenue, Dallas, TX 75206", Name = "Zephyr's Lounge", Status = "Open" });

            return Json(foo, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet);
        }

    }

    public class Foo
    {
        public Foo()
        {
            Bars = new List<Bar>();
        }

        public List<Bar> Bars { get; set; }
    }

    public class Bar
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Status { get; set; }
    }
}

2 个答案:

答案 0 :(得分:15)

答案不是很多,但是如果你将Json序列化程序从内置的(JavaScriptSerializer)切换到Json.NET(其中有一堆advantages超出此问题),你可以做一些事情像这样:

JsonConvert.SerializeObject( myObjectDestinedForJSON, Formatting.Indented);

有关设置的文档:http://james.newtonking.com/projects/json/help/

答案 1 :(得分:4)

务实且适用于所有情况:

使用chrome +此扩展程序:https://chrome.google.com/webstore/detail/chklaanhfefbnpoihckbnefhakgolnmc

当它意识到它是json时,这将是格式化JSON。