如何格式化Json输出? [英] how to format Json output?

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

问题描述

我的Web服务响应具有mimetype:"application/json",我的JSON输出不带空格,就像这样

My web service responses has mimetype: "application/json" and my JSON outputs without spacing, like this

{"Data":{"Item":"123","Timestamp":"2011-11-24T17:50:43"}}

当JSON应该这样输出时

When the JSON should output like this

{
   "Data":{
      "Item":"123",
      "Timestamp":"2011-11-24T17:50:43"
   }
}

有什么办法可以修复JSON格式,因此它看起来像#2吗?

Is there any way I can fix the JSON format, so it appears like #2?

推荐答案

我不会更改Web服务写出的格式,但是如果您要出于诊断的目的格式化它,则可以使用 Json.NET 可以很简单地做到这一点:

I wouldn't change the format written out by the web service, but if you want to format it for diagnostic purposes you can use Json.NET to do this very simply:

JObject json = JObject.Parse(text);
string formatted = json.ToString();

结果将自动格式化.您可以将其放入一个小型工具-桌面工具或某个地方的网页. (尽管已经有了在线JSON格式化程序,但我显然不会感到惊讶,尽管显然您要谨慎设置敏感数据的格式.)

The result is automatically formatted. You could put this into a small tool - either a desktop tool or a web page somewhere. (I wouldn't be surprised if there were already online JSON formatters, although obviously you'd want to be careful about formatting sensitive data.)

这篇关于如何格式化Json输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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