如何格式化Json输出? [重复]

时间:2023-01-14 10:41:32

This question already has an answer here:

这个问题在这里已有答案:

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

我的Web服务响应有mimetype:“application / json”和我的JSON输出没有间距,像这样

1

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

When the JSON should output like this

当JSON应该像这样输出

2

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

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

有什么方法可以修复JSON格式,所以它看起来像#2?

3 个解决方案

#1


22  

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:

我不会更改Web服务写出的格式,但如果您想将其格式化以用于诊断目的,您可以使用Json.NET非常简单地执行此操作:

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

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格式化器,我不会感到惊讶,尽管显然你需要小心格式化敏感数据。)

#2


2  

Jon's answer doesn't seem to work if the root element of your json is an array. Using JToken instead of JObject fixed this for me. As an extension method on string, this looks like:

如果你的json的根元素是一个数组,Jon的答案似乎不起作用。使用JToken而不是JObject为我修复了这个问题。作为字符串的扩展方法,它看起来像:

public static string FormatJson(this string json)
{
    return JToken.Parse(json).ToString();
}

#3


-1  

If you call your service from Firefox there's this nice plugin that will prettify the JSON for you: JSONView

如果你从Firefox调用你的服务,这个漂亮的插件将为你美化JSON:JSONView

I also used to use this website to format and validate any JSON: JSON Formatter

我也习惯使用这个网站来格式化和验证任何JSON:JSON Formatter

#1


22  

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:

我不会更改Web服务写出的格式,但如果您想将其格式化以用于诊断目的,您可以使用Json.NET非常简单地执行此操作:

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

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格式化器,我不会感到惊讶,尽管显然你需要小心格式化敏感数据。)

#2


2  

Jon's answer doesn't seem to work if the root element of your json is an array. Using JToken instead of JObject fixed this for me. As an extension method on string, this looks like:

如果你的json的根元素是一个数组,Jon的答案似乎不起作用。使用JToken而不是JObject为我修复了这个问题。作为字符串的扩展方法,它看起来像:

public static string FormatJson(this string json)
{
    return JToken.Parse(json).ToString();
}

#3


-1  

If you call your service from Firefox there's this nice plugin that will prettify the JSON for you: JSONView

如果你从Firefox调用你的服务,这个漂亮的插件将为你美化JSON:JSONView

I also used to use this website to format and validate any JSON: JSON Formatter

我也习惯使用这个网站来格式化和验证任何JSON:JSON Formatter