有没有特别注意简洁的 JSON 漂亮打印机?

发布于 2024-11-07 13:43:13 字数 495 浏览 3 评论 0原文

我想要一个漂亮的 JSON 打印机,它可以识别数组或对象何时适合一行并执行此操作。示例:

{
  "fits": ["JSON", "pretty", "printer"],
  "longer": [
    "???????????????????????????????????????????????????",
    "???????????????????????????????????????????????????",
    "???????????????????????????????????????????????????",
    "???????????????????????????????????????????????????",
    "???????????????????????????????????????????????????"
  ]
}

有这样的独立库吗?如果没有,我该如何去写呢?

我对 JavaScript 实现最感兴趣。

I'd like a JSON pretty printer that would recognize when an array or object fits on one line and just do that. Example:

{
  "fits": ["JSON", "pretty", "printer"],
  "longer": [
    "???????????????????????????????????????????????????",
    "???????????????????????????????????????????????????",
    "???????????????????????????????????????????????????",
    "???????????????????????????????????????????????????",
    "???????????????????????????????????????????????????"
  ]
}

Is there a standalone library like this? If not, how would I go about writing one?

I'm most interested in a JavaScript implementation.

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

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

发布评论

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

评论(3

笛声青案梦长安 2024-11-14 13:43:13

我不知道有没有这样简洁的 JSON 打印机,但如果您愿意的话,制作自己的打印机应该不难:

  • 您可以使用 for(property in object) 来迭代属性给定对象的。
    • 根据使用案例,您可能需要使用 hasOwnProperty 进行过滤。
  • 您可以使用 typeof 来确定引用是否指向对象、数组、字符串或数字
  • 让漂亮的打印机函数接收初始缩进偏移量和要打印的对象。这可能足以决定您是否应该内联每个属性。
    • 我不确定这种“贪婪”策略是否始终是“最佳” - 也许现在最好在多行中执行某些操作,以便稍后能够内联。不过一开始我并不担心这个。

I don't know about any such concise JSON printer, but it shouldn't be hard to make your own if you want to:

  • You can use the for(property in object) to iterate over the properties of a given object.
    • Depending on use case, you might want to filter with hasOwnProperty.
  • You can determine if an reference points to an object, array, string or number with typeof
  • Have your pretty printer function receive the initial indentation offset and the object to be printed. This might be enough to decide if you should inline each property or not.
    • I'm not sure this "greedy" strategy is always "optimal" - perhaps it might be better to do something in multiple lines now to be able to inline later. I wouldn't worry with this at first though.
比忠 2024-11-14 13:43:13

由于 JSON 主要是一种数据传输格式,我假设您的意思是在浏览器中查看原始 JSON?如果是这样,那么有几个选项:

你应该能够挖掘如果您需要进一步定制,请进入最后三个的源代码。我首先迭代 value.length 属性,其中 value 是数组元素,看看是否可以将输出限制为单行。

Since JSON is primarily a data transport format, I assume that you mean viewing raw JSON in the browser? If so, then there are a few options:

You should be able to dig into the source of the last three if you require further customization. I'd start with iterating through the value.length property where value is/are the array element(s) to see if you can limit your output to a single line.

燃情 2024-11-14 13:43:13

使用替换函数来比较总数每个键/值对中的字符数为固定长度。这是一个简单的例子:

function replacer(key, value)
  {
  var key_arr = [];
  var value_arr = [];
  var i = 0;
  for (_ in value)
    {
    key_arr.push(_);
    value_arr.push(value[_]);
    }
  for(;i < value_arr.length;i++)
    {
    if (key_arr[i].length + value_arr[i].length < 80)
      {
      console.log(key_arr[i] + ":" + "\t" + value_arr[i])
      }
    else
      {
      console.log(key_arr[i] + ":" + "\n" + value_arr[i])
      }
    }
  }

用法:

var json;

json = {"foo":"1","bar":"2"},      
JSON.stringify(json, replacer, 4);

json = {"foo":"12345678901234567890123456789012345678901234567890123456789012345678901234567890","bar":"2"};

JSON.stringify(json, replacer, 4);

Use a replacer function to compare the total number of characters in each key/value pair to a fixed length. Here is a simple example:

function replacer(key, value)
  {
  var key_arr = [];
  var value_arr = [];
  var i = 0;
  for (_ in value)
    {
    key_arr.push(_);
    value_arr.push(value[_]);
    }
  for(;i < value_arr.length;i++)
    {
    if (key_arr[i].length + value_arr[i].length < 80)
      {
      console.log(key_arr[i] + ":" + "\t" + value_arr[i])
      }
    else
      {
      console.log(key_arr[i] + ":" + "\n" + value_arr[i])
      }
    }
  }

Usage:

var json;

json = {"foo":"1","bar":"2"},      
JSON.stringify(json, replacer, 4);

json = {"foo":"12345678901234567890123456789012345678901234567890123456789012345678901234567890","bar":"2"};

JSON.stringify(json, replacer, 4);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文