美化 JSON 是什么意思

发布于 2024-11-01 16:57:25 字数 34 浏览 3 评论 0原文

我听说过一些美化 JSON 的网站。它到底意味着什么?

I have heard of sites that prettify/beautify JSON. What does it actually mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明月夜 2024-11-08 16:57:25

这意味着它有一个更易于人类阅读的版本。例如,以下是有效的 json,但可读性较差:

{"outcome" : "success", "result" : {"name" : "messaging-sockets", "default-interface" : "external", "include" : [], "socket-binding" : {"messaging" : {"name" : "messaging", "interface" : null, "port" : 5445, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}, "messaging-throughput" : {"name" : "messaging-throughput", "interface" : null, "port" : 5455, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}}}, "compensating-operation" : null}

经过美化后,可能如下所示:

{
   "outcome":"success",
   "result":{
      "name":"messaging-sockets",
      "default-interface":"external",
      "include":[

      ],
      "socket-binding":{
         "messaging":{
            "name":"messaging",
            "interface":null,
            "port":5445,
            "fixed-port":null,
            "multicast-address":null,
            "multicast-port":null
         },
         "messaging-throughput":{
            "name":"messaging-throughput",
            "interface":null,
            "port":5455,
            "fixed-port":null,
            "multicast-address":null,
            "multicast-port":null
         }
      }
   },
   "compensating-operation":null
}

This means a more human readable version of it. E.g. the following is valid json, but not well readable:

{"outcome" : "success", "result" : {"name" : "messaging-sockets", "default-interface" : "external", "include" : [], "socket-binding" : {"messaging" : {"name" : "messaging", "interface" : null, "port" : 5445, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}, "messaging-throughput" : {"name" : "messaging-throughput", "interface" : null, "port" : 5455, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}}}, "compensating-operation" : null}

After prettyfying this could look like this:

{
   "outcome":"success",
   "result":{
      "name":"messaging-sockets",
      "default-interface":"external",
      "include":[

      ],
      "socket-binding":{
         "messaging":{
            "name":"messaging",
            "interface":null,
            "port":5445,
            "fixed-port":null,
            "multicast-address":null,
            "multicast-port":null
         },
         "messaging-throughput":{
            "name":"messaging-throughput",
            "interface":null,
            "port":5455,
            "fixed-port":null,
            "multicast-address":null,
            "multicast-port":null
         }
      }
   },
   "compensating-operation":null
}
弄潮 2024-11-08 16:57:25

它使您的代码变得漂亮,即缩进,确保内容以类似的方式对齐,所有括号都以类似的方式放置,等等。

示例

var obj = {apple: {red: 5, green: 1}, bananas: 9}; //JS object
var str = JSON.stringify(obj, null, 4); // spacing level 4, or instead of 4 you can write "\t" for tabulator

//The third argument from stringify function enables pretty printing and sets the spacing to use.
console.log(str); //now you can see well pretty printed JSON string in console

但如果您想自己执行此操作,则可以将第二个参数用作函数。有关 JSON.stringify 函数的更多信息,您可以找到 这里

很多示例如何漂亮地打印 JSON 字符串

It makes your code pretty, i.e. it indents it, makes sure stuff is aligned in a similar manner, all parenthesis are placed in a similar manner, etc.

Example

var obj = {apple: {red: 5, green: 1}, bananas: 9}; //JS object
var str = JSON.stringify(obj, null, 4); // spacing level 4, or instead of 4 you can write "\t" for tabulator

//The third argument from stringify function enables pretty printing and sets the spacing to use.
console.log(str); //now you can see well pretty printed JSON string in console

But if you want to do it on yourself then you can use the second parameter as a function. More about JSON.stringify function you can find here.

A lot of examples how to pretty print JSON string.

后来的我们 2024-11-08 16:57:25

您可以在此处查看一个站点,该站点将美化粘贴的 JSON ...

它只是使它更具可读性,我想主要是为了调试目的。

You can see a site here that will beautify pasted JSON...

Its just makes it more readable, mainly for debugging purposed I would imagine.

幽蝶幻影 2024-11-08 16:57:25

就像@Heiko Rupp 提到的那样,它更容易阅读。大多数 JSON 文件已经经过美化。如果你想美化你自己的 JSON 格式的变量,你可以这样做:

import json

variable = {'a': 1, 'b': 2, 'c': 3}
print(variable)
# returns:
{'a': 1, 'b': 2, 'c': 3}

# prettify
print(json.dumps(variable, indent=4, sort_keys=True))
# returns:
{
    "a": 1,
    "b": 2,
    "c": 3
}

Like @Heiko Rupp mentioned, it makes easier to read. Most JSON files are already prettified. If you want to prettify your own JSON formatted variable, you can do it like so:

import json

variable = {'a': 1, 'b': 2, 'c': 3}
print(variable)
# returns:
{'a': 1, 'b': 2, 'c': 3}

# prettify
print(json.dumps(variable, indent=4, sort_keys=True))
# returns:
{
    "a": 1,
    "b": 2,
    "c": 3
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文