什么是 JSON 缩进级别约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18698738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the JSON indentation level convention?
提问by Glenn Mohammad
Is there any such thing as the "standard" convention for JSON indentation level? Should it be 2 spaces, 3 spaces, 4 spaces, tabs delimited, or something else?
是否有任何诸如 JSON 缩进级别的“标准”约定之类的东西?应该是 2 个空格、3 个空格、4 个空格、制表符分隔还是其他什么?
I tried to come across the official JSON site, but it is not stated there.
我试图找到官方的 JSON 站点,但那里没有说明。
采纳答案by Alnitak
JSON is a serializationformat, not a presentationformat.
JSON 是一种序列化格式,而不是一种表示格式。
As such, there is no "standard" indentation - JSON is typically sent as compactly as possible.
因此,没有“标准”缩进——JSON 通常尽可能紧凑地发送。
(That said, there is an option to JSON.stringify()to request "pretty printed" JSON - look at the spaceparameter at the MDN documentation)
(也就是说,有一个选项可以JSON.stringify()请求“漂亮打印”的 JSON - 查看MDN 文档中的space参数)
回答by Bergi
There is no standard. The JSON specificationpermits any number of whitespaces.
没有标准。的JSON规范允许任意数量的空格中。
However, when you are pretty-printing JSON to make it readable (e.g. in config files) it is good practise to be consistent with the coding conventions of your project and use the same indendation level as you would for an JS object literal - which is often 4 (Crockford) or 2 spaces (Node.js).
但是,当您漂亮地打印 JSON 以使其可读(例如在配置文件中)时,最好与项目的编码约定保持一致并使用与 JS 对象文字相同的缩进级别 - 即通常为 4 ( Crockford) 或 2 个空格 ( Node.js)。

